利用httpclient來(lái)模擬登陸操作

          如果有一個(gè)網(wǎng)站的url是http://xxx.xxx.xxx,登陸的動(dòng)作是login.do其中userId表示變量名 username表示內(nèi)容,密碼同理
           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 = "此網(wǎng)址內(nèi)容為空";
          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 }



          posted on 2010-03-02 13:15 Cloud kensin 閱讀(340) 評(píng)論(0)  編輯  收藏 所屬分類: Java

          <2010年3月>
          28123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(4)

          隨筆分類

          相冊(cè)

          相冊(cè)

          搜索

          最新評(píng)論

          主站蜘蛛池模板: 灵武市| 达日县| 安平县| 墨江| 电白县| 云霄县| 新疆| 海晏县| 三河市| 友谊县| 兴业县| 胶南市| 商丘市| 五寨县| 海城市| 凌云县| 四平市| 宁陵县| 旅游| 东莞市| 高邑县| 广元市| 河西区| 新蔡县| 平远县| 侯马市| 内乡县| 会泽县| 内江市| 思茅市| 沐川县| 梧州市| 金阳县| 南郑县| 博爱县| 苏尼特右旗| 正宁县| 吉首市| 长丰县| 商都县| 盐津县|