之前做过一个3版本HttpClient简单示例的示例,最近下了一个4.2.5版本的,做了一个简单的示例如下:
package com.xiva.common.util; import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIBuilder; import org.apache.http.conn.params.ConnRouteParams; import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.protocol.HttpContext; import com.xiva.common.IvExceptionCode; import com.xiva.common.bo.IvResponse; import com.xiva.exception.IvMsgException; /** * * HttpClient 服务类 * 主要负责发送http请求 * @author xiva * @version [版本号, 2013-4-30] * @see [相关类/方法] * @since [产品、模块版本] */ public class HttpClientService { private HttpClient httpClient; private HttpClientService() { httpClient = new DefaultHttpClient(); if(httpClient instanceof DefaultHttpClient) { ((DefaultHttpClient) httpClient).setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy() { @Override public long getKeepAliveDuration( HttpResponse response, HttpContext context) { long keepAlive = super.getKeepAliveDuration(response, context); if (keepAlive == -1) { keepAlive = 5000;// 会话保持时长 } return keepAlive; } }); } } public static HttpClientService getInstance(boolean useHttps) { HttpClientService service = new HttpClientService(); if (useHttps) { service.useHttpsRequest(); } service.setTimeout(); return service; } private void useHttpsRequest() { } private void setTimeout() { int timeout = 3000; // 单位毫秒 httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout); } public void useProxy(String hostName, int port) { HttpHost proxy = new HttpHost(hostName, port, "http"); httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy); } public IvResponse httpGetRequest(String url, Map<String, String> params) { IvResponse ivResponse = new IvResponse(); URIBuilder builder = new URIBuilder(); builder.setPath(url); URI uri = null; try { if (params != null && params.size() > 0) { for (Entry<String, String> entry :params.entrySet()) { builder.setParameter(entry.getKey(), entry.getValue()); } } uri = builder.build(); } catch (URISyntaxException e1) { throw new IvMsgException(IvExceptionCode.SYS_ERROR); } HttpGet httpGet = new HttpGet(uri); HttpResponse httpRes = null; try { httpRes = httpClient.execute(httpGet); ivResponse.setHttpRes(httpRes); } catch (Exception e) { throw new IvMsgException(IvExceptionCode.SYS_ERROR); } return ivResponse; } public IvResponse httpPostRequest(String url, Map<String, String> params) { IvResponse ivResponse = new IvResponse(); HttpPost httpPost = new HttpPost(url); if (params != null && params.size() > 0) { List<NameValuePair> formparams = new ArrayList<NameValuePair>(); for (Entry<String, String> entry :params.entrySet()) { formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } try { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8"); httpPost.setEntity(entity); } catch (UnsupportedEncodingException e) { throw new IvMsgException(IvExceptionCode.SYS_ERROR); } } HttpResponse httpRes = null; try { httpRes = httpClient.execute(httpPost); ivResponse.setHttpRes(httpRes); } catch (Exception e) { throw new IvMsgException(IvExceptionCode.SYS_ERROR); } return ivResponse; } }
这个版本与之前3的版本相比较,多了Fluent和Cache功能。
相关推荐
NULL 博文链接:https://mr--fang.iteye.com/blog/1562669
HttpClient已经应用在很多的项目中,比如Apache Jakarta上很著名的另外两个开源项目Cactus和HTMLUnit都使用了HttpClient。 适用人群:JavaWeb开发者 使用场景:在Java后端程序中,模拟浏览器端进行Web请求。 其它:...
Commons HTTPClient4.X组件应用示例
一个使用HttpClient访问WS 的例子,包含相关jar包
httpclient的简单示例,httpclient相对较复杂的,http协议就有大量内容,本资源只是对httplclient jar的简单使用
服务器端自己写,本示例为客户端代码,可以解决httpClient4乱码问题,是替代修改源码的最佳写法。
HttpClient接口调用工具类 (Post请求 get请求 put请求 delete请求 下载就可以用 )
主要介绍了spring boot封装HttpClient的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了php实现httpclient类示例,需要的朋友可以参考下
关于HttpClient的小例子,很简单,附件为项目源码,直接导入执行。注意包含了配置信息。
本篇文章主要介绍了angular5 httpclient的示例实战,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
hp实现httpclient类示例,需要的朋友可以参考下,buffer 获取返回的字符串,referer 设置 HTTP_REFERER 的网址,response 服务器响应的 header 信息,request 发送到服务器的 header 信息。
目录前言正文OkHttp使用示例源码解析Apache HttpClient使用示例源码解析GoogleHttpClient哪个更香?总结声明 前言 前八篇文章介绍完了feign-core核心内容,从本篇开始将介绍它的“其它模块”。其实核心模块可以独立...
http 、https客户端,实现了get方法和post方法,可以上传和下载文件
HttpClient抓取网页Demo,HttpClient 的入门示例,配合blog文章的附件
主要介绍了php实现httpclient类示例,需要的朋友可以参考下,buffer 获取返回的字符串,referer 设置 HTTP_REFERER 的网址,response 服务器响应的 header 信息,request 发送到服务器的 header 信息。
httpClient连接https 获得验证码图片示例 需要证书才能连接的那种 /* 本文所用开发工具 jak1.5.0_06 eclipse:ObjectWeb Lomboz lib: commons-codec-1.4.jar commons-logging-1.1.jar httpclient-4.0.3.jar ...
java 访问 wcf 示例(使用的HttpClient)
该项目是使用版本6.0.8生成的。 开发服务器 为开发服务器运行ng serve 。... 如果您更改任何源文件,该应用程序将自动重新加载。 代码脚手架 运行ng generate component component-name生成一个新的组件。...