在这里插入代码片
public static String Http_Post(String strurl,String xml){
HttpURLConnection connection = null;
InputStream is = null;
BufferedReader br = null;
String result = null;// 返回结果字符串
Date d1 = new Date();
try {
// 创建远程url连接对象
URL url = new URL(strurl);
// 通过远程url连接对象打开一个连接,强转成httpURLConnection类
connection = (HttpURLConnection) url.openConnection();
//connection.setSSLSocketFactory(new TLSSocketConnectionFactory());
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
// 设置连接方式:GET,POST
connection.setRequestMethod("POST");
// 设置连接主机服务器的超时时间:15000毫秒
connection.setConnectTimeout(15000);
// 设置读取远程返回的数据时间:60000毫秒
connection.setReadTimeout(60000);
// 发送请求
connection.connect();
OutputStream out = connection.getOutputStream(); // 获取输出流对象
connection.getOutputStream().write(xml.getBytes("UTF-8")); // 将要提交服务器的SOAP请求字符流写入输出流
out.flush();
out.close();
// System.out.println("输出流"+connection.getResponseCode());
// 通过connection连接,获取输入流
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
// 封装输入流is,并指定字符集
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
// 存放数据
StringBuffer sbf = new StringBuffer();
String temp = null;
while ((temp = br.readLine()) != null) {
sbf.append(temp);
sbf.append("");
}
result = sbf.toString();
}
}catch (SocketTimeoutException e) {
System.out.println("****** SocketTimeoutException ********");
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭资源
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
connection.disconnect();// 关闭远程连接
}
Date d2 = new Date();
//System.out.println(d2.getTime() - d1.getTime());
//System.out.println("****** END ********");
//System.out.println();
return result;
}
仅针对POST请求
带Basic 账号密码
public static String Http_Post(String strurl,String xml,String username,String password){
HttpURLConnection connection = null;
InputStream is = null;
BufferedReader br = null;
String result = null;// 返回结果字符串
Date d1 = new Date();
try {
// 创建远程url连接对象
URL url = new URL(strurl);
// 通过远程url连接对象打开一个连接,强转成httpURLConnection类
connection = (HttpURLConnection) url.openConnection();
//connection.setSSLSocketFactory(new TLSSocketConnectionFactory());
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
// 设置连接方式:GET,POST
connection.setRequestMethod("POST");
// 设置连接主机服务器的超时时间:15000毫秒
connection.setConnectTimeout(15000);
// 设置读取远程返回的数据时间:60000毫秒
connection.setReadTimeout(60000);
//使用base64进行加密
byte[] tokenByte = Base64.encodeBase64((username+":"+password).getBytes());
//将加密的信息转换为string
String tokenStr = new String(tokenByte,"UTF-8");
String token = "Basic "+tokenStr;
connection.setRequestProperty("Authorization",token);
// 发送请求
connection.connect();
OutputStream out = connection.getOutputStream(); // 获取输出流对象
connection.getOutputStream().write(xml.getBytes("UTF-8")); // 将要提交服务器的SOAP请求字符流写入输出流
out.flush();
out.close();
//System.out.println("输出流"+connection.getResponseCode());
// 通过connection连接,获取输入流
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
// 封装输入流is,并指定字符集
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
// 存放数据
StringBuffer sbf = new StringBuffer();
String temp = null;
while ((temp = br.readLine()) != null) {
sbf.append(temp);
sbf.append("");
}
result = sbf.toString();
}
}catch (SocketTimeoutException e) {
System.out.println("****** SocketTimeoutException ********");
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭资源
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
connection.disconnect();// 关闭远程连接
}
Date d2 = new Date();
//System.out.println(d2.getTime() - d1.getTime());
//System.out.println("****** END ********");
//System.out.println();
return result;
}