Migrating from HttpClient 3.1 to HttpClient 4.0
来源:互联网 发布:淘宝机器人在哪 编辑:程序博客网 时间:2024/05/01 10:17
We were using Apache HttpClient 3.1 for some time now and have decide to bite the bullet and migrate over to HttpClient 4.0.3. It’s usually painless to update Apache libraries, but HttpClient 4 was a complete rewrite and is not backward compatible. It’s also now a top level Apache project and no longer part of Apache Commons. Here are the things we had to change to migrate over to the latest HttpClient. This is a mini HttpClient 4 tutorial for those moving to the latest version. The time it takes you to migrate depends on how many references you have to change. It took us less than 30 minutes to do it on our project.
1. Replace commons-httpclient-3.1.jar with the latest httpclient-4.0.3.jar and httpmime-4.0.3.jar. You will also need httpcore-4.0.1.jar.
2. Change your import statements from org.apache.commons.httpclient.* to import org.apache.http.*. For example, change:
import
org.apache.commons.httpclient.HttpClient;
import
org.apache.commons.httpclient.HttpStatus;
import
org.apache.commons.httpclient.HttpException;
import
org.apache.commons.httpclient.methods.GetMethod;
import
org.apache.commons.httpclient.ConnectTimeoutException;
to
import
org.apache.http.client.HttpClient;
import
org.apache.http.HttpStatus;
import
org.apache.http.HttpException;
import
org.apache.http.client.methods.HttpGet;
import
org.apache.http.conn.ConnectTimeoutException;
import
org.apache.http.HttpResponse;
import
org.apache.http.impl.client.DefaultHttpClient;
Notice the addition of HttpResponse and DefaultHttpClient, which will be used next.
3. Change your code from the old HttpClient interface to the new.
HttpClient 3.1:
HttpClient client =
new
HttpClient();
GetMethod method =
new
GetMethod(url);
int
statusCode = client.executeMethod(method);
if
(statusCode == HttpStatus.SC_OK) {
InputStream is = method.getResponseBodyAsStream();
// do something with the input stream
}
method.releaseConnection();
HttpClient 4.0:
HttpClient client =
new
DefaultHttpClient();
HttpGet method =
new
HttpGet(url);
HttpResponse httpResponse = client.execute(method);
int
statusCode = httpResponse.getStatusLine().getStatusCode();
if
(statusCode == HttpStatus.SC_OK) {
InputStream is = httpResponse.getEntity().getContent();
// do something with the input stream
}
We certainly did not use the full functionality of HttpClient, and most of our changes were similar to those listed above. For a complete examples on how to use HttpClient 4, visit the HttpClient 4.0.3 Tutorial. Do you have any other tips for HttpClient 3 to 4 migration?
- Migrating from HttpClient 3.1 to HttpClient 4.0
- ★★ httpclient-3.1 TO HttpClient 4.0.1,httpclient-3.1 升级到HttpClient 4.0.1
- Migrating from VC6 to VC9
- WARNING:keystoneclient.httpclient:Failed to retrieve management_url from token
- Migrating from 1.0.x to 1.5 Stable
- Migrating Databases To and From ASM with
- Migrating Queries from One User To Another
- 【书籍推荐】Migrating to Swift from Android
- migrating from ia32 to arm digest
- [转]Migrating From Eclipse to IntelliJ IDEA
- Migrating From Eclipse to IntelliJ IDEA
- HttpClient
- HttpClient
- httpClient
- HttpClient
- HttpClient
- HttpClient
- HttpClient
- 如何正确的对待设计模式——我的观点
- py2exe ImportError: No module named iterators,ImportError: MemoryLoadLibrary failed loading win32ap
- 如何在sencha-touch (暂时) simon grid column中放置button 并弹出popup界面
- C#数据库操作类类DB_Class
- AIX 挂载nfs提示vmount: Not owner
- Migrating from HttpClient 3.1 to HttpClient 4.0
- 不要用float和double来进行精确的小数计算
- 尝试对struts2返回的JSON数据进行压缩
- 在vs2005调试asp程序
- 商业应用中Java浮点数的精确计算及表示
- 一步步讲解如何调试vxworks网卡驱动-适合嵌入式初学者
- 测试的
- 13个对Android开发者有帮助的工具和资源
- 如何在运行时添加classpath