Apache HttpComponents Client 4.0快速入门/升级-1.GET方法访问网页
2009-11-02 13:58
489 查看
Apache HttpComponents Client 4.0已经发布多时,httpclient项目从commons子项目挪到了HttpComponents子项目下,httpclient3.1和httpcilent4.0无法做到代码向后兼容,升级比较麻烦。我在做项目之余找时间研究了一下,写了一套3.1与4.0对比的代码,不求面面俱到,但求简单易懂。如果代码用到真实项目中,还需要考虑诸如代理、Header、异常处理之类的问题。
Http GET方法得到www.g.cn的源码:
Http GET方法得到www.g.cn的源码:
import java.io.IOException; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; public class GetSample { /** * @param args * @throws IOException * @throws HttpException */ public static void main(String[] args) throws HttpException, IOException { String url = "http://www.g.cn/"; System.out.println(url); System.out.println("Visit google using Apache commons-httpclient 3.1:"); System.out.println(get3(url)); System.out.println("Visit google using Apache HttpComponents Client 4.0:"); System.out.println(get4(url)); } /** 使用Apache commons-httpclient 3.1,GET方法访问网页 */ public static String get3(String url) throws HttpException, IOException { org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient(); GetMethod getMethod = new GetMethod(url); try { if (httpClient.executeMethod(getMethod) != HttpStatus.SC_OK) { System.err.println("Method failed: " + getMethod.getStatusLine()); } return getMethod.getResponseBodyAsString(); } finally { getMethod.releaseConnection(); } } /** 使用Apache HttpComponents Client 4.0,GET方法访问网页 */ public static String get4(String url) throws ClientProtocolException, IOException { org.apache.http.client.HttpClient client = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url); try { return client.execute(httpget, new BasicResponseHandler()); } finally { client.getConnectionManager().shutdown(); } } }
相关文章推荐
- Apache HttpComponents Client 4.0快速入门/升级-2.POST方法访问网页
- Apache HttpComponents Client 4.0快速入门/升级-2.POST方法访问网页
- HttpComponents Client 4.0快速入门/升级-2.POST方法访问网页
- HttpComponents(Apache HttpComponents Client 4.1.3)通过代理访问网页的设置方法
- HttpComponents(Apache HttpComponents Client 4.1.3)通过Post方式访问网页或传输参数
- apache 访问网站forbidden: client denied by server configuration错误解决方法
- Apache HttpComponents 的使用入门(java使用HttpClient访问Api, 使用json.org库处理json数据)
- java应用集锦9:httpclient4.2.2的几个常用方法,登录之后访问页面问题,下载文件
- Apache设置禁止使用IP访问网站的方法
- apache 启动后访问网页提示You don't have permission to access / on this server.
- 访问chm文件出现 已取消到该网页的导航的解决方法
- Apache HttpComponents在App里访问HTTP服务
- 无法访问HttpRequestBase 找不到org.apache.http.client.methods.HttpRequestBase的类文件
- windows下不打开浏览器访问网页的方法
- Apache Shiro 快速入门教程,shiro 基础教程
- 【centos安装wordpress】要执行请求的操作,WordPress 需要访问您网页服务器的权限。 请输入您的 FTP 登录XXXX”完美解决方法
- Android 6.0删除Apache HttpClient相关类的解决方法
- 在OpenSuse Linux下升级firefox至4.0版本的方法(备忘)
- Apache Geode 快速入门(15分钟