Http请求工具类


package com.yxd.pris.common.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 
 *功能说明:HTTP工具类
 *
 *
 *创建时间:2017年7月17日 下午5:48:15
 *
 *修改人             修改时间             修改描述
 *
 *
 *
 */
public class HttpUtils {

	/**
	 * 
	 *功能说明:拼接头部
	 *输入参数:
	 *输出参数:
	 *创建时间:2018-1-30 下午2:20:18
	 *
	 */
	public static Map<String, String> createHeaders(String info)
	{
		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Content-Type", "application/json");
		headers.put("Accept", "application/json");
		try
		{
			headers.put("Content-Length", String.valueOf(info.getBytes("UTF8").length));
		} catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}

		return headers;
	}
	
	/**
	 * 
	 *功能说明:发起HTTP请求
	 *输入参数:请求地址URL
	 *输出参数:请求参数
	 *创建时间:2017年7月17日 下午5:48:47
	 *
	 */
	public static String sendPost(String url, String charset,String params) {  
	//请求头配置
	Map<String, String> headers = createHeaders(params);
        String result = null;
        InputStream in = null;
        OutputStreamWriter writer = null;
        try {
            URL uul = new URL(url);
            HttpURLConnection urlcon = (HttpURLConnection) uul.openConnection();
	    // 添加请求头
	    Set keys = headers.keySet();
	    for (String key : keys)
		{
	           urlcon.setRequestProperty(key, headers.get(key));
		}
            urlcon.setDoOutput(true);   //需要输出
            urlcon.setDoInput(true);   //需要输入
            urlcon.setUseCaches(false);  //不允许缓存
            urlcon.setConnectTimeout(30*1000);
            urlcon.setReadTimeout(30*1000);
            urlcon.setRequestMethod("POST");
            urlcon.connect();// 获取连接
            //入参
            writer = new OutputStreamWriter(urlcon.getOutputStream(),charset);
		    writer.write(params);
		    writer.flush();
            //出参
            in = urlcon.getInputStream();
            BufferedReader buffer = new BufferedReader(new InputStreamReader(in, charset));
            StringBuffer bs = new StringBuffer();
            String line = null;
            while ((line = buffer.readLine()) != null) {
                bs.append(line);
            }
            result = bs.toString();
        } catch (Exception e) {
            System.out.println("[请求异常][地址:" + url + "][错误信息:" + e.getMessage() + "]");
        } finally {
            try {
                if (null != writer) {
                	writer.close();
                }            	
                if (null != in) {
                    in.close();
                }
            } catch (Exception e2) {  
                System.out.println("[关闭流异常][错误信息:" + e2.getMessage() + "]");  
            }
        }  
        return result;  
    }
	
	/**
	 * 
	 *功能说明:get方式查询
	 *输入参数:url 编码
	 *输出参数:请求结果 
	 *创建时间:2017-12-14 下午5:49:04
	 *
	 */
	public static String sendGet(String url, String charset) {  
        String result = null;
        InputStream in = null;
        OutputStreamWriter writer = null;
        try {
            URL uul = new URL(url);
            HttpURLConnection urlcon = (HttpURLConnection) uul.openConnection();
            urlcon.setDoOutput(true);   //需要输出
            urlcon.setDoInput(true);   //需要输入
            urlcon.setUseCaches(false);  //不允许缓存
            urlcon.setConnectTimeout(30*1000);
            urlcon.setReadTimeout(30*1000);
            urlcon.setRequestMethod("GET");
            urlcon.connect();// 获取连接
            //出参
            in = urlcon.getInputStream();
            BufferedReader buffer = new BufferedReader(new InputStreamReader(in, charset));
            StringBuffer bs = new StringBuffer();
            String line = null;
            while ((line = buffer.readLine()) != null) {
                bs.append(line);
            }
            result = bs.toString();
        } catch (Exception e) {
            System.out.println("[请求异常][地址:" + url + "][错误信息:" + e.getMessage() + "]");
        } finally {
            try {
                if (null != writer) {
                	writer.close();
                }            	
                if (null != in) {
                    in.close();
                }
            } catch (Exception e2) {  
                System.out.println("[关闭流异常][错误信息:" + e2.getMessage() + "]");  
            }
        }  
        return result;  
    }	
}

装载请标明出处: http://www.yueshuge.cn

发表评论

电子邮件地址不会被公开。 必填项已用*标注