Http请求辅助工具HttpClient
来源:互联网 发布:创维电视连接有线网络 编辑:程序博客网 时间:2024/05/16 04:48
/**
* @功能描述:http请求辅助工具
*/
@Component
public class HttpUtil{
privateLog logger = LogFactory.getLog(getClass());// 日志
/**
* @功能描述:http执行get方法
*/
publicString get(String url) {
Stringtext = "";
HttpClientclient = HttpClients.createDefault();
HttpGetget = new HttpGet("http://localhost:8080" + url);
HttpResponseresponse;
try{
response= client.execute(get);
HttpEntityentity = response.getEntity();
text= EntityUtils.toString(entity);
}catch (ClientProtocolException e) {
logger.error("http执行get请求出现ClientProtocolException异常");
e.printStackTrace();
}catch (IOException e) {
logger.error("http执行get请求出现IOException异常");
e.printStackTrace();
}finally {
get.releaseConnection();//释放连接
}
returntext;
}
/**
* @功能描述:http执行post方法
*/
publicString post(String url, Map<String, String> map) {
Stringtext = "";
HttpClientclient = HttpClients.createDefault();
HttpPostpost = new HttpPost("http://localhost:8080" + url);
try{
List<NameValuePair>params = new ArrayList<>();// 定义名值对类型的list
for(Map.Entry<String, String> entry : map.entrySet()) {
params.add(newBasicNameValuePair(entry.getKey(), entry.getValue()));
}
HttpEntityhttpEntity = new UrlEncodedFormEntity(params, "utf-8");//参数重新编码并放到请求实体中
post.setEntity(httpEntity);//把请求实体放到post请求中
HttpResponseresponse = client.execute(post);
HttpEntityentity = response.getEntity();
text= EntityUtils.toString(entity);
}catch (ClientProtocolException e) {
logger.error("http执行get请求出现ClientProtocolException异常");
e.printStackTrace();
}catch (IOException e) {
logger.error("http执行get请求出现IOException异常");
e.printStackTrace();
}finally {
post.releaseConnection();//释放连接
}
returntext;
}
}
- Http请求辅助工具HttpClient
- HttpClient触发HTTP请求
- httpClient 发送 http请求
- http请求步骤(httpclient)
- HTTPClient发送http请求
- HttpClient 发送HTTP请求
- Httpclient发送http请求
- HttpClient发送HTTP请求
- Http请求接口HttpClient
- HttpClient模拟发送http请求
- android HttpClient Http请求 验证
- httpclient支持https,http请求
- HttpClient (HTTP 请求工具类)
- Android HTTP请求方式:HttpClient
- 使用HttpCLient处理HTTP请求
- Android HTTP请求方式:HttpClient
- HttpClient 发送 HTTP、HTTPS 请求
- HttpClient发送HTTP请求详解
- 学习笔记之前端库/框架
- Filter过滤器
- 凸包问题
- Unity3d的父级与Destory()
- 分治法-归并排序
- Http请求辅助工具HttpClient
- java动态代理例子(Proxy,InvocationHandler)
- HDU 2089 不要62 (数位DP)
- 用Editplus开发HTML
- hdu5000一道很有意思的DP
- 作为人才我们为什么要和几个猎头保持良好的关系?
- 实战演练:用gulp像CMS那样生成新闻页面
- AOP编程+动态代理
- ionic 使用genymotion模拟器