HttpResponse httpResponse=null;
HttpEntity httpEntity=null;
//生成一个请求对象 参数为请求的地址
HttpGet httpGet=new HttpGet("http://www.baidu.com");
//生成一个HttpClient客户端对象
HttpClient httpClient=new DefaultHttpClient();
InputStream inputStream=null;
try {
//使用http客户端发送请求对象到服务器
httpResponse=httpClient.execute(httpGet);
//包含服务器返回的内容
httpEntity=httpResponse.getEntity();
//从流中服出内容
inputStream=httpEntity.getContent();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
String result="";
String line="";
while((line=bufferedReader.readLine())!=null){
result+=line;
}
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(inputStream!=null){
inputStream.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
java使用HttpClient发送http请求
最新推荐文章于 2024-06-25 14:35:48 发布