这里我们是将我们发送的报文信息写入到流里面,在对方获取我们的参数信息或者报文信息时需要从流里面获取我们发送的信息。
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
/**
* HTTP请求链接工具类
* @author zhangpengliang
* @version 2017-09-27
*
*/
public class HttpConnectUtil {
/***
* 判断接受地址是否连接
*
* @param urlPath
* @return boolean
*/
public static boolean isSuccessConnected(String urlPath){
URL url;
try {
url = new URL(urlPath);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setConnectTimeout(4000);
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.connect();
int responseCode = connection.getResponseCode();
if(responseCode == 200){
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* POST 发送请求
* @param urlPath 链接地址URL
* @param xml 报文结构xml
* @return
*/
public static String post(String urlPath, String xml){
OutputStreamWriter out = null;
if(StringUtil.isEmpty(urlPath)){
return null;
//需要抛出一个错误
}
try {
URL url = new URL(urlPath);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setConnectTimeout(4000);
//下面就是添加头信息
connection.setRequestProperty("Content-Type","text/xml");
connection.setRequestProperty("charset", "UTF-8");
connection.connect();
out = new OutputStreamWriter(connection.getOutputStream());
out.write(xml);
out.flush();
out.close();
//读取响应
InputStream inputStream = connection.getInputStream();//真正发送请求
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes());
sb.append(lines);
}
System.out.println(sb.toString());
reader.close();
// 断开连接
connection.disconnect();
return sb.toString();
}catch(MalformedURLException ee){
System.out.println(ee);
}catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}finally{
if(out != null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
/**
* POST 发送请求
* @param urlPath 链接地址
* @param xml 报文结构xml
* @param param 头部信息参数
* @return
*/
public static String post(String urlPath, String xml,Map<String, Object> param){
OutputStreamWriter out = null;
if(StringUtil.isEmpty(urlPath)){
return null;
//需要抛出一个错误
}
try {
URL url = new URL(urlPath);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setConnectTimeout(4000);
//下面就是添加头信息
connection.setRequestProperty("Content-Type","text/xml");
connection.setRequestProperty("charset", "UTF-8");
for(String s:param.keySet()){
connection.setRequestProperty(s, param.get(s).toString());
}
connection.connect();
out = new OutputStreamWriter(connection.getOutputStream());
out.write(xml);
out.flush();
out.close();
//读取响应
InputStream inputStream = connection.getInputStream();//真正发送请求
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes());
sb.append(lines);
}
System.out.println(sb.toString());
reader.close();
// 断开连接
connection.disconnect();
return sb.toString();
}catch(MalformedURLException ee){
System.out.println(ee);
}catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}finally{
if(out != null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
Http请求工具类---将请求信息写入流
最新推荐文章于 2023-01-06 09:31:32 发布