问题描述
- java中HttpGet和HttpClient和HttpResponse之间的联系是如何的
-
java中HttpGet和HttpClient和HttpResponse之间的联系是如何的
每一个对象分别承载着什么信息
解决方案
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
时间: 2024-10-28 01:05:51