2 u011359326 u011359326 于 2016.02.25 17:41 提问

java中HttpGet和HttpClient和HttpResponse之间的联系是如何的

java中HttpGet和HttpClient和HttpResponse之间的联系是如何的
每一个对象分别承载着什么信息

1个回答

Mr_dsw
Mr_dsw   Ds   Rxr 2016.02.25 17:48
已采纳
 public class HTTPGetSample {  
    public static void main(String[] args) throws ClientProtocolException, IOException {  
        String url = "http://www.google.com.hk/search?q=httpClient";  

        HttpClient client = new DefaultHttpClient();  
        HttpGet request = new HttpGet(url);  

        HttpResponse response = client.execute(request);  
        System.out.println("Response Code: " +  
        response.getStatusLine().getStatusCode());  

        BufferedReader rd = new BufferedReader(  
            new InputStreamReader(response.getEntity().getContent()));  
        String line = "";  
        while((line = rd.readLine()) != null) {  
        System.out.println(line);  
        }  
    }  
} 

在这个例子中可以看出,HttpGet是我们请求的类型,为GET请求,然后通过HttpClient执行HttpGet请求,返回HttpResponse

Csdn user default icon
上传中...
上传图片
插入图片