HttpClient Examples:Response handling

官方主頁:http://hc.apache.org/

 

Components

Response handling

This example demonstrates how to process HTTP responses using a response handler. This is the recommended way of executing HTTP requests and processing HTTP responses. This approach enables the caller to concentrate on the process of digesting HTTP responses and to delegate the task of system resource deallocation to HttpClient. The use of an HTTP response guarantees that the underlying HTTP connection will be released back to the connection manager automatically in all cases.

 

這個例子示範怎樣去操作處理Http的請求應答。這里推薦的方式是執行HTTP請求和處理HTTP應答。这种做法使呼叫者把注意力集中在處理过程中HTTP响应,并委派的任务,系统资源释放到HttpClient 。使用HTTP响应的根本保证, HTTP连接将被释放回连接管理器会自动在所有情况下。

 

 

package cn.lake.util;

import org.apache.http.client.ResponseHandler;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 * This example demonstrates the use of the {@link ResponseHandler} to simplify 
 * the process of processing the HTTP response and releasing associated resources.
 */
public class ClientWithResponseHandler {

    public final static void main(String[] args) throws Exception {
        
        HttpClient httpclient = new DefaultHttpClient();

        HttpGet httpget = new HttpGet("http://www.google.com/"); 

        System.out.println("executing request " + httpget.getURI());

        // Create a response handler
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httpget, responseHandler);
        System.out.println(responseBody);
        
        System.out.println("----------------------------------------");
    }
    
}

 

 

(httpclient.execute(httpget,responseHandler );  )有報錯,原因還在查之中!

 

還有代碼前面那些注解,都是法律與叫你捐點錢,有心人可以去看;

 

翻譯的不好,請見諒!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值