博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java Http请求工具类
阅读量:6846 次
发布时间:2019-06-26

本文共 2977 字,大约阅读时间需要 9 分钟。

hot3.png

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

你可能感兴趣的文章
Failure is not fatal, but failure to change might be.
查看>>
L2-015. 互评成绩
查看>>
iOS9新特性
查看>>
poj3186 poj3267
查看>>
烂泥:学习centos之快速搭建LNMP环境
查看>>
Poj2723:Get Luffy Out
查看>>
L365
查看>>
SUST OJ 1642: 绝地求生—死亡顺序
查看>>
Android中XML解析-Dom解析
查看>>
highcharts 多数据+切换
查看>>
关于输入输出及编译优化 模板代码
查看>>
世界上各种壮观震撼奇景。也许你这辈子都看不到了!
查看>>
安装包制作工具 SetupFactory使用2 API清单
查看>>
js中with、this的用法
查看>>
ADO.NET调用存储过程
查看>>
安装UBUNTU Server 11.10
查看>>
MVVM架构~使用boxy和knockoutjs实现编辑功能
查看>>
Web analytics unique visitors go sky high 网站分析报表,唯一IP地址用户
查看>>
MVC之参数验证(三)
查看>>
android截图 - 截取ContentView - 截取指定的View并且保存
查看>>