HttpTool.java 【暂保留】
备注
在 java tool util 工具类 中已存在
HttpTool.java
该类为java源生态的http 请求工具,不依赖第三方jar包 ,即插即用.
package kingtool; import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
/**
* http post发送工具
* @author King
*
*/
public class HttpTool {
public static void main(String[] args) throws Exception {
String requestUrl = "http://192.168.1.2/httpserver";
String requestData = readStringFromFile("C:\\Users\\King\\Desktop\\connectANSI.xml","GBK");//有乱码,请修改指定编码
//_________________________________________________________________________________________
String returnData=HttpTool.sendRequestData("telesales",requestData, requestUrl,"GBK","GBK", ,);//大家最终只要使用这一句代码就可调用
//_________________________________________________________________________________________
} /**
* 发送报文
*
* @param appName 应用系统英文名
* @param requestData 请求报文
* @param urlStr 请求地址
* @param connectionTimeout 链接超时时间 1000代表 1秒
* @param readTimeout 读取超时时间 1000代表1秒
* @return
* @throws IOException
* @author King
*/
public static String sendRequestData(String appName,String requestData, String urlStr,String sendEncoding,String recvEncoding, int connectionTimeout,int readTimeout) throws IOException{
URL url = null;
HttpURLConnection conn = null;
ByteArrayOutputStream byteOut = null;
BufferedReader readInfo = null;
StringBuffer strBuilder=new StringBuffer();
OutputStream out = null;
try {
System.out.println("请求时间:【"+new Date()+"】");
System.out.println("请求地址:【"+urlStr+"】");
System.out.println("请求报文:【"+requestData+"】");
url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("SOAPAction", "\"\"");
conn.setRequestProperty("Accept", "application/soap+xml, application/dime, multipart/related, text/*");
//如果没有下面这一行代码,服务器端可以通过request.getParameter()和request.getInputStream()都接收到相同信息
//conn.setRequestProperty("content-type", "text/xml;charset=GBK");
//如果 有上面这一行代码,服务器端仅能通过request.getInputStream()接收信息
conn.setRequestProperty("User-Agent", "Axis/1.4");
conn.setRequestProperty("Cache-Control", "no-cache");
conn.setRequestProperty("appName", appName);//各系统需要设置应用系统名 appName,如电销为telesales
conn.setUseCaches(false); //忽略缓存
conn.setDoOutput(true); //使用 URL 连接进行输出
conn.setDoInput(true); //使用 URL 连接进行输入
conn.setConnectTimeout(connectionTimeout);//链接超时
conn.setReadTimeout(readTimeout);//读取超时
conn.connect();//建立链接
byteOut = new ByteArrayOutputStream();
byteOut.write(requestData.getBytes(sendEncoding));//以指定编码发送,如果有乱码,修改之
byte[] buf = byteOut.toByteArray();
out = conn.getOutputStream();
out.write(buf);
out.flush();
if (HttpURLConnection.HTTP_OK == conn.getResponseCode()) {//正确返回
readInfo = new BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),recvEncoding));//以指定编码读取返回信息,如果有乱码,修改之
String line = null;
while ((line = readInfo.readLine()) != null) {
strBuilder.append(line);
}
} else {//没有正确返回
readInfo = new BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),recvEncoding));//以指定编码读取返回信息,如果有乱码,修改之
System.out.println("出现异常,返回报文:【"+readInfo+"】");
throw new IOException("url请求出现问题,返回编码:" + conn.getResponseCode());
}
System.out.println("返回时间:【"+new Date()+"】");
System.out.println("返回报文:【"+strBuilder.toString()+"】");
} catch (UnsupportedEncodingException e) {
throw e;
} catch (MalformedURLException e) {
throw e;
} catch (IOException e) {
throw e;
}finally {
try{
if (readInfo != null) {
readInfo.close();
}
if (byteOut != null) {
byteOut.close();
}
if (out != null) {
out.close();
}
if (conn != null) {
conn.disconnect();
}
}catch(Exception e){
System.out.println("关闭链接出错!"+e.getMessage());
} }
return strBuilder.toString();
} /**
*
* @param filePath 文件绝对路径
* @param encoding 读取文件的编码
* @return
* @author King
* @throws Exception
*/
public static String readStringFromFile(String filePath,String encoding) {
File file = new File(filePath);
System.out.println("文件 "+filePath+"存在与否?: "+ file.exists()+"\n");
String tempLine = null;
String retStr = "";
InputStreamReader isr = null;//way1:
// FileReader fr = null;//way2
StringBuilder sb = new StringBuilder();
try {
if(file.exists()){
isr = new InputStreamReader(new FileInputStream(file),encoding);//way1:
// fr = new FileReader(file);//way2
BufferedReader br = new BufferedReader(isr);//way1:
// BufferedReader br = new BufferedReader(fr);;//way2:
tempLine = br.readLine();
while( tempLine != null ){
sb.append(tempLine);
tempLine = br.readLine();
}
retStr = sb.toString();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try{
if(isr!=null)
isr.close();
}catch(Exception e){
e.printStackTrace();
}
}
System.out.println("读到的文件内容如下:");
System.out.println(retStr+"\n");
return retStr;
}
}
HttpTool.java 【暂保留】的更多相关文章
- HttpTool.java(在java tool util工具类中已存在) 暂保留
HttpTool.java 该类为java源生态的http 请求工具,不依赖第三方jar包 ,即插即用. package kingtool; import java.io.BufferedReader ...
- Java float保留两位小数或多位小数
Java float保留两位小数或多位小数 方法1:用Math.round计算,这里返回的数字格式的. float price=89.89;int itemNum=3;float totalPr ...
- java double保留小数点的零的问题,java保留小数点问题
1.用DecimalFormat格式化,DecimalFormat df=new DecimalFormat("0.00"); System.out.println(df.form ...
- Java四舍五入 保留小数
java 四舍五入保留小数 // 方式一: double f = 3.1516; BigDecimal b = new BigDecimal(f); double f1 = b.setScale( ...
- JAVA除法保留小数点后两位的两种方法 Java Math的 floor,round和ceil的总结
floor 返回不大于的最大整数 round 则是4舍5入的计算,入的时候是到大于它的整数round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下 ...
- java中保留几位小数
public class NumUtils { /** * 保留两位小数 * * @param d * @return */ public static String get2Wei(double d ...
- BigDecimal 高精度计算 熟悉扩展,java除法保留小数问题
java保留两位小数问题: 方式一: 四舍五入 double f = 111231.5585; BigDecimal b = new BigDecimal(f); d ...
- Java指定保留小数位数的方法
package com.qiyuan.util; import java.math.BigDecimal; import java.math.RoundingMode; import java.tex ...
- java double 保留两位小数
java保留两位小数问题: 方式一: 四舍五入 double f = 111231.5585; BigDecimal b = new BigDecimal(f); d ...
随机推荐
- HTML 5 placeHolder
<html> <body> <input type="text" id="idNum" placeholder="pla ...
- JS创建事件的三种方式(实例)
1.普通的定义方式 <input type="button" name="Button" value="确定" onclick=&qu ...
- MySql连接空闲8小时自动断开引起的问题
一.问题描述 最近遇到了一个奇怪的MySql数据库问题,好几次前一天晚上历史数据还正常存储,第二天早上来了看实时数据存储还正常,历史数据不存储了.找了好久也没找到问题.后来仔细想了想,历史数据设置 ...
- Node params和query的Get请求传参
//1:加载http express框架//2:创建服务器const http = require("http");const express = require("ex ...
- 无法获得锁 /var/lib/dpkg/lock
sudo rm /var/cache/apt/archives/lock sudo rm /var/lib/dpkg/lock
- js break和continue
break的兩種形式: break:未加標籤,適用於switch和循環結構 break labelname:適用於任何代碼塊: continue的兩種形式: continue:適用於循環結構 cont ...
- [UVALive 3661] Animal Run
图片加载可能有点慢,请跳过题面先看题解,谢谢 附:中文题面,[BZOJ1001]狼抓兔子 就要考联赛了,博客里题目的\(style\)都变了,几乎都是些套路啥的,这道题也比较套路 第一眼看这道题的感觉 ...
- 洛谷 P2887 [USACO07NOV]防晒霜Sunscreen 解题报告
P2887 [USACO07NOV]防晒霜Sunscreen 题目描述 To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2 ...
- shell(1)-磁盘shell
查看硬盘的大小脚本[root@localhost ~]# vi repboot.sh#!/bin/bash# To show usage of /boot directory and mode of ...
- luogu4187 [USACO18JAN]Stamp Painting (dp)
可以发现,只要存在连续k个相同的,这个情况就一定是合法情况 然而这个不太好算,我们算不存在k个相同的,然后用$m^n$把它减掉 设f[i]为前i个,没有连续k个的 显然$f[i]=m^i ,i< ...