首页 » Java » 一个简易Java模拟Http请求封装工具类

一个简易Java模拟Http请求封装工具类

原文 http://blog.csdn.net/hero_lxz/article/details/79163828

2018-01-25 17:39:47阅读(464)

package com.lxz.Http;
import Java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
 * 模拟发送Http请求
 * @author linxz
 */
public class HttpUtil {
	
	/**
	 * 发送post请求
	 * @param sendUrl 要发送请求到的http地址
	 * @param params 要附带的请求的参数
	 * @param headers 要设置的请求头
	 * @param getDataCharset 最后获取的返回的内容的字符集,默认为utf-8
	 * @return
	 */
	public static String sendPost(String sendUrl,String params,Map<String,String> headers,String getDataCharset){
		StringBuffer result=new StringBuffer();
		PrintWriter out=null;
		OutputStream os=null;
		BufferedReader br=null;
		InputStreamReader isr=null;
		InputStream in=null;
		try {
			URL url=new URL(sendUrl);
			URLConnection conn=url.openConnection();
			conn.setDoOutput(true);
	        conn.setDoInput(true);
	        Set<String> headerskey=headers.keySet();
	        for(String headerkey:headerskey){
	        	conn.setRequestProperty(headerkey, headers.get(headerkey));
	        }
	        os=conn.getOutputStream();
	        out=new PrintWriter(os);
			out.print(params);
			out.flush();
			in=conn.getInputStream();
			if(getDataCharset==null||"".equals(getDataCharset.trim())){
				getDataCharset="utf-8";
			}
			isr=new InputStreamReader(in, getDataCharset);
			br=new BufferedReader(isr);
			String line=null;
			while((line=br.readLine())!=null){
				result.append(line);
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {//释放资源
			if(out!=null){
				out.close();
			}
			if(os!=null){
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(br!=null){
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(isr!=null){
				try {
					isr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(in!=null){
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result.toString();
	}
	
	/**
	 * 发送get请求
	 * @param sendUrl 要发送请求到的http地址
	 * @param headers 要设置的请求头
	 * @param getDataCharset 最后获取的返回的内容的字符集,默认为utf-8
	 * @return
	 */
	public static String sendGet(String sendUrl,Map<String,String> headers,String getDataCharset){
		StringBuffer result=new StringBuffer();
		BufferedReader br=null;
		InputStreamReader isr=null;
		InputStream in=null;
		try{
			URL url=new URL(sendUrl);
			URLConnection conn=url.openConnection();
			Set<String> headerskey=headers.keySet();
	        for(String headerkey:headerskey){
	        	conn.setRequestProperty(headerkey, headers.get(headerkey));
	        }
	        in=conn.getInputStream();
			if(getDataCharset==null||"".equals(getDataCharset.trim())){
				getDataCharset="utf-8";
			}
			isr=new InputStreamReader(in, getDataCharset);
			br=new BufferedReader(isr);
			String line=null;
			while((line=br.readLine())!=null){
				result.append(line);
			}
		}catch (Exception e) {
		}finally {
			if(br!=null){
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(isr!=null){
				try {
					isr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(in!=null){
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result.toString();
	}
	
	
	public static void main(String[] args) {
		Map<String,String> headers=new HashMap<>();
		//headers.put("Context-Type", "application/json;charset=utf-8");
		String s=sendGet("https://www.baidu.com/", headers, "utf-8");
		System.out.println(s);
	}
}

最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹