Http请求工具类---将请求信息写入流

这里我们是将我们发送的报文信息写入到流里面,在对方获取我们的参数信息或者报文信息时需要从流里面获取我们发送的信息。

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;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值