jetty client 与apache http client的实现、分析

简介:

谈到httpclient的话,只要会想到apache的httpclient和jetty的httpclient,但是apache的httpclient3和4之间又有区别,通过学些,最终总结了三种方式使用HttpClient,

分别为使用httpclient3,httpclient4,jetty的httpclient,下面分别来贴代码:


第1种:使用的jar包为commons-httpclient-3.1,只需要一个jar包即可

这里使用的是GetMethod,与httpcleint4有区别

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public  static  void  main(String[] args) {
    HttpClient httpClient =  new  HttpClient();   //打开窗口
    GetMethod getMethod =  new  GetMethod( "http://www.baidu.com/" ); //输入网址
   try  {
      int  statusCode = httpClient.executeMethod(getMethod);   //按下回车运行,得到返回码
      System.out.println(statusCode);
      if  (statusCode != HttpStatus.SC_OK) {
          System.err.println( "Method failed: "  + getMethod.getStatusLine());
     
       }
      //读取内容 
      byte [] responseBody = getMethod.getResponseBody();   //得到返回的内容
      //处理内容
      System.out.println( new  String(responseBody));   
catch  (Exception e) {
    e.printStackTrace();
} finally {
    getMethod.releaseConnection();
   }
}


第二种,使用的jar包为httpclient4,需要的jar包由httpclient3分成了多个,自己去下载

   注意这里使用的是HttpGet,而httpclient3使用的是GetMethod

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  public  static  void  main(String[] args) {
      HttpClient httpClient =  new  DefaultHttpClient();
      HttpGet httpGet =  new  HttpGet( "http://www.baidu.com" );
      // 打印请求信息
      System.out.println(httpGet.getRequestLine());
      try  {
          // 发送请求,返回响应
          HttpResponse response =httpClient.execute(httpGet);
         // 打印响应信息
            System.out.println(response.getStatusLine());
            HttpEntity httpEntity = response.getEntity();
           System.out.println( httpEntity.getContentType());
           System.out.println(httpEntity.getContentLength());
           System.out.println(EntityUtils.getContentCharSet(httpEntity));
           InputStream in = httpEntity.getContent();   //可以得到请求的内容了
           .....                                     //这里可以自由发挥了
        
      catch  (Exception e) {
      }
  
    }

第三种:使用jetty的httpclient,jar在jetty包里面的lib包里面

    可以分为同步和异步两种

 异步:

   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public  static  void  main(String[] args) {
   HttpClient client =  new  HttpClient();
   client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);   //还可以进行其他的配置
   try  {
    client.start();
     catch  (Exception e) {
     e.printStackTrace();
     }
     
ContentExchange exchange =  new  ContentExchange()    //exchange,里面放置回调方法
{
      protected  void  onResponseComplete()  throws  IOException
      {
        super .onResponseComplete();     
        String responseContent =  this .getResponseContent();
                System.out.println(responseContent);
        ........                                   //可以对返回结果自由发挥了
      }
};
exchange.setMethod( "GET" );
exchange.setURL( "http://www.baidu.com" );
try  {
client.send(exchange);                       //client发送消息
catch  (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

同步:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
HttpClient httpClient =  new  HttpClient();
     ........................... //同样对client进行配置
try  {
     httpClient.start();
     ContentExchange contentExchange =  new  ContentExchange();
     httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
     contentExchange.setURL( "http://www.baidu.com" );
     contentExchange.setTimeout( 30000 );
     httpClient.send(contentExchange);     //client发送
     contentExchange.waitForDone();        //同步等待结果返回
     System.err.println( "Response status: " +contentExchange.getResponseStatus());
     System.out.println( "Response content:" +contentExchange.getResponseContent());
catch  (Exception e) {
     e.printStackTrace();
}

希望对初学者有所帮助!!!!!




     本文转自布拉君君 51CTO博客,原文链接:http://blog.51cto.com/5148737/1413767,如需转载请自行联系原作者






相关文章
|
2月前
|
安全 Apache
Metasploit -- 对Apache HTTP服务器守护进程中断复现
Metasploit -- 对Apache HTTP服务器守护进程中断复现
20 0
|
3月前
|
缓存 网络协议 数据库连接
C/S架构中HTTP错误状态码原因分析及解决办法
HTTP(Hypertext Transfer Protocol)是用于在客户端和服务器之间传输数据的协议。当在浏览器或其他HTTP客户端中访问网页时,可能会发生各种访问报错。我们需要根据网页提供的错误状态码分析错误原因,以找到相对应的解决办法。
41 0
|
3月前
|
移动开发 JSON 监控
网络协议解析:在员工上网监控软件中实现HTTP流量分析
随着企业对员工网络活动的监控需求不断增加,开发一套能够实现HTTP流量分析的网络协议解析系统变得愈发重要。本文将深入探讨如何在员工上网监控软件中实现HTTP流量分析,通过代码示例演示关键步骤。
221 0
|
3月前
|
XML 存储 网络协议
kettle开发篇-Http client
kettle开发篇-Http client
89 0
|
23天前
|
内存技术
HTTP-FLV详解及分析(二)
HTTP-FLV详解及分析
23 0
|
1月前
|
Shell Linux 网络安全
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 httpd命令 使用指南
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 httpd命令 使用指南
30 0
|
1月前
|
Shell Linux Apache
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 apachectl命令 使用教程
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 apachectl命令 使用教程
162 1
|
2月前
|
Java Apache
Apache HttpClient 4.5设置超时时间
Apache HttpClient 4.5设置超时时间
|
2月前
|
前端开发 应用服务中间件 网络安全
在Apache上http强制跳转到https无效的解决办法
在Apache上http强制跳转到https无效的解决办法
|
4月前
|
数据采集 安全 Java
Kotlin+Apache HttpClient+代理服务器=高效的eBay图片爬虫
本文将为你介绍一种高效的eBay图片爬虫的实现方式,让你可以用Kotlin+Apache HttpClient+代理服务器的组合来轻松地下载eBay的图片。
Kotlin+Apache HttpClient+代理服务器=高效的eBay图片爬虫

推荐镜像

更多