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,如需转载请自行联系原作者






相关文章
|
5天前
|
存储 Apache
Apache Hudi Savepoint实现分析
Apache Hudi Savepoint实现分析
42 0
|
5天前
|
Apache 索引
精进Hudi系列|Apache Hudi索引实现分析(五)之基于List的IndexFileFilter
精进Hudi系列|Apache Hudi索引实现分析(五)之基于List的IndexFileFilter
24 0
|
5天前
|
Apache
Apache Hudi Rollback实现分析
Apache Hudi Rollback实现分析
27 0
|
5天前
|
应用服务中间件 网络安全 nginx
Client sent an HTTP request to an HTTPS server
Client sent an HTTP request to an HTTPS server
13 0
|
5天前
|
JSON Java Apache
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
|
5天前
|
Shell Linux 网络安全
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 httpd命令 使用指南
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 httpd命令 使用指南
36 0
|
5天前
|
Shell Linux Apache
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 apachectl命令 使用教程
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 apachectl命令 使用教程
168 1
|
5天前
|
存储 SQL 消息中间件
Apache Hudi:统一批和近实时分析的存储和服务
Apache Hudi:统一批和近实时分析的存储和服务
45 0
|
5天前
|
缓存 Apache 索引
Apache Hudi索引实现分析(一)之HoodieBloomIndex
Apache Hudi索引实现分析(一)之HoodieBloomIndex
26 0
|
5天前
|
Apache 索引
Apache Hudi索引实现分析(二)之HoodieGlobalBloomIndex
Apache Hudi索引实现分析(二)之HoodieGlobalBloomIndex
30 0

热门文章

最新文章

推荐镜像

更多