Java Http请求工具类

public static String httpPost(String source, String params) {
		URL url = null;
		HttpURLConnection conn = null;
		OutputStream os = null;
		String ret = null;
		try {
			url = new URL(source);
			conn = (HttpURLConnection) url.openConnection();
			addRequestHeader(conn);
			conn.setRequestMethod("POST");
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setUseCaches(false);
			conn.connect();
			if (params != null) {
				os = conn.getOutputStream();
				os.write(params.getBytes("UTF-8"));
				os.flush();
			}
			InputStream is = conn.getInputStream();
			ret = getResponse(is);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (conn != null) {
				conn.disconnect();
			}
		}
		return ret;
	}
	public static String httpGet(String source) {
		URL url = null;
		HttpURLConnection conn = null;
		String ret = null;
		try {
			url = new URL(source);
			conn = (HttpURLConnection) url.openConnection();
			addRequestHeader(conn);
			conn.setRequestMethod("GET");
			conn.setDoInput(true);
			conn.setUseCaches(false);
			conn.connect();
			InputStream is = conn.getInputStream();
			ret = getResponse(is);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (conn != null) {
				conn.disconnect();
			}
		}
		return ret;
	}
	
	public static String getResponse(InputStream is) {
		GZIPInputStream gzip = null;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		String ret = null;
		try {
			gzip = new GZIPInputStream(is);
			int len = 0;
			byte[] buffer = new byte[1024];
			while ((len = gzip.read(buffer)) != -1) {
				baos.write(buffer, 0, len);
			}
			baos.flush();
			ret = new String(baos.toByteArray(), "UTF-8");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (baos != null) {
					baos.close();
				}
				if (gzip != null) {
					gzip.close();
				}
				if (is != null) {
					is.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return ret;
		
	}
	
	public static void addRequestHeader(HttpURLConnection conn) {
		conn.setRequestProperty("Accept", "text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8");
		conn.setRequestProperty("Accept-Charset", "utf-8, iso-8859-1;q=0.5");
		conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
		conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
		conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0");
	}

 

转载于:https://my.oschina.net/yuewawa/blog/2208154

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值