【学习】从HttpClient3迁移到HttpClient4
网上很多HttpClient的学习源码都是3版本的,不过,既然现在HttpClient已经升级成Apache的顶级项目,自然想使用新版本。但是4版本完全是重新编写,而不能向下兼容。所以在学习调试源码时,利用的4的库,就不能直接运行3.1下的源码。
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;??使用的各个HttpClient的功能的代码修改。这部分的整理,在使用过程中慢慢补全。本次先介绍Get方法的修改。
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();ToHttpClient4.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}?哪位有更完整的资料,求分享!?