您的位置:首页 > 理论基础 > 计算机网络

HttpClient访问网络

2014-09-28 16:51 337 查看
HttpClient项目时Apache提供用于访问网络的类,对访问网络的方法进行了封装。在HttpURlConnection类中的输入输出操作,统一封装成HttpGet、HttpPost、HttpResponse

一、服务器端前台

1、发送GET请求的步骤:

(1)创建HttpClient对象:HttpClient httpClient=new DefaultHttpClient();

(2)创建HttpGet对象:HttpGet httpGet=new HttpGet("http://www.teachcourse.cn");

(3)添加发送参数:httpGet.setParams("姑娘的眼睛真大,好漂亮咯!");

(4)调用HttpClient对象的execute()方法发送请求:HttpResponse httpResponse=httpClient.execute();

2、案例演示:

HttpClient httpClient=new DefaultHttpClient();

HttpGet httpGet=new HttpGet("http://www.teachcourse.cn");

try{

HttpResponse httpResponse=httpClient.execute();

if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){//解释服务器响应

result=EntityUtils.toString(httpResponse.getEntiry());//获取返回的字符串

}else{result="请求失败!";}

}catch(ClientProtocolException e){

e.printStackTrace();

}catch(IOException e){e.printStackTrace();};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: