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();};