博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
阅读量:6003 次
发布时间:2019-06-20

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

hot3.png

package com.icss.daas.core.util;import java.util.Iterator;import java.util.Map;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.httpclient.methods.PostMethod;import org.apache.commons.httpclient.params.HttpMethodParams;import org.apache.logging.log4j.Logger;import com.alibaba.fastjson.JSONObject;import org.apache.logging.log4j.LogManager;public class HttpClientUtil {	static Logger logger = LogManager.getLogger(HttpClientUtil.class);	//static Logger logger = LogManager.getLogger(HttpClientUtil.class);	private static String DEFAULT_CHARSET = "UTF-8";	private static int DEFAULT_CONNECTION_TIMEOUT = 10 * 1000;	private static int DEFAULT_SO_TIMEOUT = 10 * 1000;	public static String addUrl(String head, String tail) {		if (head.endsWith("/")) {			if (tail.startsWith("/")) {				return head.substring(0, head.length() - 1) + tail;			} else {				return head + tail;			}		} else {			if (tail.startsWith("/")) {				return head + tail;			} else {				return head + "/" + tail;			}		}	}	/**	 * post请求数据	 * @param url	 * @param params	 * @param charset	 * @return	 * @throws Exception	 */	public synchronized static String postData(String url, JSONObject json, String charset) throws Exception {		//构造HttpClient的实例 		HttpClient httpClient = new HttpClient();		//创建post方法的实例 		PostMethod method = new PostMethod(url);		charset=StringUtils.isBlank(charset)? DEFAULT_CHARSET:charset;				((PostMethod) method).addParameter("json", json.toString());  		HttpMethodParams httpMethodParam = method.getParams();  		httpMethodParam.setContentCharset("UTF-8");  		httpMethodParam.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset);		String result = "";		try {			httpClient.executeMethod(method);// 执行postMethod 			result=new String(method.getResponseBody(),charset);		} catch (Exception e) {			throw e;		}finally{			//释放连接 			method.releaseConnection();		}		return result;	}		public synchronized static String postData(String url, Map
header,JSONObject json, String charset) throws Exception { //构造HttpClient的实例 HttpClient httpClient = new HttpClient(); //创建post方法的实例 PostMethod method = new PostMethod(url); charset=StringUtils.isBlank(charset)? "utf-8":charset; ((PostMethod) method).addParameter("cdata", json.toString()); HttpMethodParams httpMethodParam = method.getParams(); httpMethodParam.setContentCharset("UTF-8"); httpMethodParam.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset); // 添加请求头 Iterator
iter = header.keySet().iterator(); String key = null; while(iter.hasNext()) { key = iter.next(); method.addRequestHeader(key,header.get(key)); } String result = ""; try { httpClient.executeMethod(method);// 执行postMethod result=new String(method.getResponseBody(),charset); } catch (Exception e) { logger.error("HTTP以POST请求数据异常", e); throw e; }finally{ //释放连接 method.releaseConnection(); } return result; } /** * get请求数据 * @param url * @param params * @param charset * @return * @throws Exception */ public synchronized static String getData(String url, Map
params, String charset) throws Exception { final HttpClient httpClient = new HttpClient(); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT); httpClient.getHttpConnectionManager().getParams().setSoTimeout(DEFAULT_SO_TIMEOUT); final GetMethod method = new GetMethod(url); String result = ""; try { httpClient.executeMethod(method); charset=StringUtils.isBlank(charset)? DEFAULT_CHARSET:charset; result=new String(method.getResponseBody(),charset); } catch (Exception e) { logger.info("HTTP以GET请求数据异常", e); throw e; }finally{ method.releaseConnection(); } return result; }}

 

转载于:https://my.oschina.net/u/3053442/blog/1555454

你可能感兴趣的文章
jieba库使用和好玩的词云
查看>>
POJ 1905:Expanding Rods 求函数的二分
查看>>
css基础示例代码
查看>>
我的Python成长之路---第八天---Python基础(25)---2016年3月5日(晴)
查看>>
ubuntu14.04+cuda8.0+caffe+opencv2.4.13+matlab安装指南
查看>>
矩阵图中的广度优先搜索
查看>>
C# 全角和半角转换以及判断的简单代码
查看>>
正则表达式
查看>>
插件推荐系列
查看>>
An introduction to parsing text in Haskell with Parsec
查看>>
Redis在java开发中使用
查看>>
input file样式美化
查看>>
博客园页面设置
查看>>
docker环境搭建
查看>>
开发过程中,ps要做的事情
查看>>
[IOS] Storyboard全解析-第一部分
查看>>
CSS:opacity 的取值范围是 0~1
查看>>
Silverlight 自定义的附加属性
查看>>
常见问题
查看>>
Sqlite插入或更新
查看>>