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 );  )有報錯,原因還在查之中!

 

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

 

翻譯的不好,請見諒!

 

你可能感兴趣的:(apache,Google)