大家好,又见面了,我是你们的朋友全栈君。
如果周期比较长的项目,或者这个项目开发人员换过了好几拨人,很有可能出现一些奇怪的问题,比如一个项目中出现了多种Spring注入bean的方式,不同版本的jar冲突等等
爬虫项目有的时候更是过犹不及,拿模拟登陆来说,开发人员的迭代,每个人的风格和技术各不相同,模拟登陆的方式也是五花八门,早在之前看到过一个项目的源码,其中使用HttpClient也是各种风格,虽然官方已经强烈建议使用HttpClient 4.x之后版本,但是在该项目中均存在HttpClient 3.x的和HttpClient 4.x的创建Httpclient对象。造成风格不一,维护繁杂,而新进项目的同学又很难进行区分不易上手,是有可能继续使用旧代码造成不良循环。
本文针对以上的情况进行了详细讲述:
1. 关于HttpClient 3.x 和 HttpClient 4.x项目的历史
如下图所示:
原Commons HttpClient project(HttpClient 3.x) 是 Jakarta Commons 项目下的一个子项目,在2007年Commons项目独立,Commons摒弃了Httpclient 3.x,不再对其维护;而在早些年2005年Jakarta成立了子项目HttpComponents项目用于开发HttpClients 3.x的替代项目HttpClient 4.x。就在Commons项目脱离Jakarta项目的同年2007年,HttpComponents也脱离Jakarta项目成为Apache顶级项目,进行开发维护HttpClient 4.x及后续版本。
从Httpclient历史中可以看出早在2005年Apache就有了要取代3.x的打算,成立了单独项目HttpComponents,并在两年后发布4.x版本取代了3.x。
Apache官方公告链接:https://hc.apache.org/httpclient-3.x/
2. 区分HttpClient 3.x 和 HttpClient 4.x 的jar包
如下图所示,如果是HttpClient 3.x 在项目中需要引入 import org.apache.commons.httpclient。而HttpClient 4.x 则需要引入 import org.apache.http.client.HttpClient。
另外关于3.x和4.x的jar包依赖,在上图中可以看出,3.x的jar依赖于commons-logging和commons-codec。而4.x依赖于httpcore、commons-logging和commons-codec。从下图的maven依赖中也可以清晰的看出区分。
3. HttpClient 3.x 升级到 4.x 的参照表总结如下:
Commons HttpClient 3.x | HttpComponents HttpClient 4.x | |
---|---|---|
import | import org.apache.commons.httpclient.Cred;font-family:Consolas, SimSun, Helvetica, sans-serif;font-size:13px;entials; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HostConfiguration; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient. MultiThreadedHttpConnectionManager; import org.apache.commons.httpclient. UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.DeleteMethod; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; | import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.auth.AuthScope; import org.apache.http.auth.Credentials; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.params.ConnRoutePNames; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; |
HttpClient | HttpClient client = new HttpClient(connectionManager); | DefaultHttpClient client = new DefaultHttpClient(); // 4.3版本前 // 4.3版本后的创建方式见表下方代码 |
proxy configuration | client.getHostConfiguration().setProxy( proxyHost, proxyPort); client.getState().setAuthenticationPreemptive( true); Credentials cred = new UsernamePasswordCredentials(proxyUser, proxyPassword); client.getState().setProxyCredentials(AuthScope.ANY_HOST, proxyHost, cred) | client.getCredentialsProvider().setCredentials( new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword)); HttpHost proxy = new HttpHost(proxyHost proxyPort); client.getParams().setParameter( ConnRoutePNames.DEFAULT_PROXY, proxy); |
GET | GetMethod getMethod = new GetMethod(); getMethod.setPath(url.getFile()); | HttpGet httpGet = new HttpGet(url.getFile()); |
GET Header | getMethod.setRequestHeader(key, value); | httpGet.addHeader(key, value); |
Execute GET | client.executeMethod(getMethod); | HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); HttpResponse httpResponse = client.execute( targetHost, httpGet); |
POST | PostMethod post = new PostMethod(); post.setPath(url.getFile()); | HttpPost post = new HttpPost(url.getFile()); |
POST Header | post.setRequestHeader(key, (String) headers.get(key)); | post.addHeader(key, (String) headers.get(key)); |
POST Body | post.setRequestBody(message); | StringEntity messageEntity = new StringEntity( message, ContentType.create(“text/plain”, “UTF-8”)); post.setEntity(messageEntity); |
Execute POST | client.executeMethod(post); | HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); HttpResponse httpResponse = client.execute(targetHost, post); |
Response Header | Header[] rspHeaders = post.getResponseHeaders(); | Header[] rspHeaders = httpResponse.getAllHeaders(); |
Response Body | String responseMsg = post.getResponseBodyAsString() | StringBuffer buffer = new StringBuffer(); BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent())); String dataLine = null; while((dataLine = reader.readLine()) != null){ buffer.append(dataLine); } String responseMsg = buffer.toString(); |
在上表中需要注意的是:如果是HttpClient 4.3之后的版本,创建HttpClient对象的方法有如下几种:
// 一般默认的创建HC对象的方法,实际上底层实现为:HttpClientBuilder.create().build()
CloseableHttpClient httpClient = HttpClients.createDefault();
// HttpClientBuilder 创建HC对象的各种方式
CloseableHttpClient closeableHttpClient = new HTTPClientBuilder().build();
CloseableHttpClient closeableHttpClient = new HTTPClientBuilder().withCookiesStore().build();
CloseableHttpClient closeableHttpClient = new HTTPClientBuilder().withGlobalConfig().build();
CloseableHttpClient closeableHttpClient = new HTTPClientBuilder().withSllContext().build();
CloseableHttpClient closeableHttpClient = new HTTPClientBuilder().withCookiesStore()
.withGlobalConfig()
.withSllContext()
.build();
4. HttpClient 3.x 和 4.x 废弃API一览:
在每个版本的HttpClient中都有Deprecated list(废弃API一览),下面的官方连接中可以方便查询到官方不建议使用的API,这些API如果继续使用会和JDK的API一样出现删除线。
3.x 版本 https://hc.apache.org/httpclient-3.x/apidocs/deprecated-list.html
4.x 版本 https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/deprecated-list.html
5. HttpClient 3.x 和 4.x 的常量变化一览:
在无论是3.x还是4.x的版本中,默认都定义了常量文件,里面提供了默认的状态码,协议头等等的常量,这样一些常用的就不需要自己再次定义了,可以直接使用HttpClient的常量类直接来使用,具体的详细常量参照下面的链接:
3.x 版本 https://hc.apache.org/httpclient-3.x/apidocs/constant-values.html
4.x 版本 https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/constant-values.html
6. HttpClient 3.x 和 4.x 超时Timeout设置变化:
HttpClient 3.x
HttpClient client = new HttpClient();
client.setConnectionTimeout(5000);
client.setTimeout(5000);
HttpClient httpClient= new HttpClient();
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
HttpClient 4.x到HttpClient4.3以下
HttpClient httpClient=new DefaultHttpClient();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,5000);
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,5000);
HttpClient4.3以上
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("https://www.csdn.net/");
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
httpGet.setConfig(requestConfig);
httpClient.execute(httpGet);
7. 对于HttpClient 4.5.x 之后的版本废弃API版本替代策略(针对SSL相关API)
HttpClient4.5之后依然有很多的变化,其中一些API也废弃掉了。
下文针对 4.x 以后 SSL证书验证相关废弃(@Deprecated)API提供了最新的替换策略。
HttpClient 4.5.x 之后 Deprecated 废弃API 的替代对应策略:https://blog.csdn.net/dietime1943/article/details/86705539
8. 其他一些有用的链接:
HttpClient官方指南教程:https://hc.apache.org/httpcomponents-client-ga/tutorial/html/index.html
What does setDefaultMaxPerRoute and setMaxTotal mean in HttpClient?
https://stackoverflow.com/questions/30689995/what-does-setdefaultmaxperroute-and-setmaxtotal-mean-in-httpclient
注:本文原创由`bluetata`发布于blog.csdn.net、转载请务必注明出处。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/163261.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...