httpClient 模拟表单提交

  1. httpClient 模拟表单提交  
  2.   
  3.   
  4. public static void diandianAdd(int i) {  
  5.         String url = "http://www.";  
  6.         PostMethod postMethod = new PostMethod(url);  
  7.         // 填入各个表单域的值  
  8.         NameValuePair[] data = {  
  9.                 new NameValuePair("email""aaaaaaaaaa"+i+"@gmall.com"),  
  10.                 new NameValuePair("passwd""aaaaaaaaaa"+i),  
  11.                 new NameValuePair("passwd_r""aaaaaaaaaa"+i),  
  12.                 new NameValuePair("uname""aaaaaaaaaa"+i),};  
  13.         // 将表单的值放入postMethod中  
  14.         postMethod.setRequestBody(data);  
  15.         // 执行postMethod  
  16.         int statusCode = 0;  
  17.         try {  
  18.             statusCode = httpClient.executeMethod(postMethod);  
  19.         } catch (HttpException e) {  
  20.             e.printStackTrace();  
  21.         } catch (IOException e) {  
  22.             e.printStackTrace();  
  23.         }  
  24.         // HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发  
  25.         // 301或者302  
  26.         if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY  
  27.                 || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {  
  28.             // 从头中取出转向的地址  
  29.             Header locationHeader = postMethod.getResponseHeader("location");  
  30.             String location = null;  
  31.             if (locationHeader != null) {  
  32.                 location = locationHeader.getValue();  
  33.                 System.out.println("diandianAdd:" + location);  
  34.             } else {  
  35.                 System.err.println("Location field value is null.");  
  36.             }  
  37.             return;  
  38.         } else {  
  39.             System.out.println(postMethod.getStatusLine());  
  40.             String str = "";  
  41.             try {  
  42.                 str = postMethod.getResponseBodyAsString();  
  43.             } catch (IOException e) {  
  44.                 e.printStackTrace();  
  45.             }  
  46. //            System.out.println(str);  
  47.         }  
  48.         postMethod.releaseConnection();  
  49.         return;  
  50.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值