博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jetty client 与apache http client的实现、分析
阅读量:7067 次
发布时间:2019-06-28

本文共 3252 字,大约阅读时间需要 10 分钟。

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

你可能感兴趣的文章
ES6走走看看—字符到底发生了什么变化
查看>>
Java基本问题
查看>>
Hexo搭建GitHub博客—打造炫酷的NexT主题--高级(三)
查看>>
内部技术分享PPT ------ “小程序入门”
查看>>
一款小程序增强开发工具 - EWA
查看>>
基于vue2.X的webpack基本配置(手动版~)
查看>>
观察者模式
查看>>
《Thinking in Android》 - 博客索引(Android 9.0)
查看>>
【开源】Tsar——灵活的系统和应用采集软件
查看>>
async/await 并行请求和错误处理
查看>>
深入浅出 Node ( 二 ) 模块机制
查看>>
【跃迁之路】【493天】刻意练习系列252(2018.06.13)
查看>>
TiDB 在威锐达 WindRDS 远程诊断及运维中心的应用
查看>>
安装 VMware workstation
查看>>
javascript 判断是否为数组 isArray()
查看>>
php脚本开发Alfred workflow
查看>>
scrapy-redis分布式爬虫框架详解
查看>>
golang 的channels 行为
查看>>
Visual Studio Code 断点调试 Vue
查看>>
checkbox jquery 全选反选
查看>>