HttpClient下载图片和向服务器提交数据实例

来源:互联网 发布:golang defer func 编辑:程序博客网 时间:2024/05/16 08:58
使用 HttpClient 需要以下 6 个步骤:
1. 创建 HttpClient 的实例
2. 创建某种连接方法的实例,在这里是GetMethod。在 GetMethod 的构造函数中传入待连接的地址
3. 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例
4. 读 response
5. 释放连接。无论执行方法是否成功,都必须释放连接
6. 对得到后的内容进行处理
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. public class DemoHttpClient03 {  
  2.     public static void main(String[] args) throws ClientProtocolException, IOException {  
  3.           
  4.         //1,导包  
  5.         //2,得到HttpClient对象  
  6.             HttpClient client = new DefaultHttpClient();  
  7.               
  8.         //3,设置请求方式  
  9.             HttpGet get = new HttpGet("http://photocdn.sohu.com/20150610/mp18368185_1433925691994_5.jpg");  
  10.           
  11.         //4,执行请求, 获取响应信息  
  12.             HttpResponse response = client.execute(get);  
  13.               
  14.             if(response.getStatusLine().getStatusCode() == 200)  
  15.             {  
  16.                 //得到实体  
  17.                 HttpEntity entity = response.getEntity();  
  18.                   
  19.                 byte[] data = EntityUtils.toByteArray(entity);  
  20.                   
  21.                 //图片存入磁盘  
  22.                 FileOutputStream fos = new FileOutputStream("d:/mpl.jpg");  
  23.                 fos.write(data);  
  24.                 fos.close();  
  25.                   
  26.                 System.out.println("图片下载成功!!!!");     
  27.             }  
  28.     }  
  29. }  

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. public class DemoHttpClient04 {  
  2.     public static void main(String[] args) throws ClientProtocolException, IOException {  
  3.         //1, 导包  
  4.         //2, 得到HttpClient对象  
  5.             HttpClient client = new DefaultHttpClient();  
  6.         //3, 设置请求方式 post  
  7.             HttpPost post = new HttpPost("http://localhost:8080/Day_28_Servlet/LoginServlet");  
  8.         //6, List<BasicNameValuePair>  
  9.             List<BasicNameValuePair> parameters = new ArrayList();  
  10.             BasicNameValuePair p1 = new BasicNameValuePair("useName""abc");  
  11.             parameters.add(p1);  
  12.               
  13.             BasicNameValuePair p2 = new BasicNameValuePair("usePwd""123");  
  14.             parameters.add(p2);  
  15.               
  16.         //5, 请求"实体" (封装请求参数的对象)  
  17.             HttpEntity entity = new UrlEncodedFormEntity(parameters);  
  18.         //4, 需要给post中加入参数  
  19.             post.setEntity(entity);  
  20.   
  21.         //7, 执行请求, 获取响应  
  22.             HttpResponse response = client.execute(post);  
  23.           
  24.             if(response.getStatusLine().getStatusCode() ==200)  
  25.             {  
  26.                 //得到响应的实体  
  27.                 HttpEntity responseEntity  = response.getEntity();  
  28.                   
  29.                 String str = EntityUtils.toString(responseEntity);  
  30.                   
  31.                 System.out.println("响应的内容为 : " + str);  
  32.             }  
  33.     }  
  34. }  
0 0
原创粉丝点击