利用httpclient来模拟登陆操作

如果有一个网站的url是http://xxx.xxx.xxx,登陆的动作是login.do其中userId表示变量名 username表示内容,密码同理
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->  1 public static String getHttpContent(String loginForm, String url, String userId, String passwordId,
 2             String username, String password) throws Exception {
 3         String responseString = "";
 4         HttpClient conn = new HttpClient();
 5         if (!loginForm.equals("")) {
 6             UTF8PostMethod signinPost = new UTF8PostMethod(loginForm);
 7             signinPost.addParameter(userId, username);
 8             signinPost.addParameter(passwordId, password);
 9             conn.executeMethod(signinPost);
10         }
11         UTF8PostMethod postMethod = new UTF8PostMethod(url);
12         int statusCode = conn.executeMethod(postMethod);
13         if (statusCode > 300) {
14             Header header = postMethod.getResponseHeader("location");
15             String location = null;
16             if (header != null) {
17                 location = header.getValue();
18                 System.out.println("The page was redirected to:" + location);
19                 postMethod = new UTF8PostMethod(location);
20             }
21         }
22         String serverPath = HttpUtils.getServerContext(url);
23         String host = HttpUtils.gethost(url);
24         String[] item = url.split("/");
25         responseString = postMethod.getResponseBodyAsString();
26         if (responseString == null)
27             responseString = "此网址内容为空";
28         if (item.length > 4) {
29             if (responseString.replace("/" + item[3], item[0+ "//" + item[2+ "/" + item[3]) != null) {
30                 responseString = responseString.replace("/" + item[3], item[0+ "//" + item[2+ "/" + item[3]);
31             }
32         } else {
33             if (responseString.replace("/" + item[3], item[0+ "//" + item[2]) != null) {
34                 responseString = responseString.replace("/" + item[3], item[0+ "//" + item[2]);
35             }
36         }
37 return responseString;

38  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值