首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

HttpClient登陆163 容易例子

2012-12-23 
HttpClient登陆163 简单例子import org.apache.commons.httpclient.Cookie import org.apache.commons.ht

HttpClient登陆163 简单例子

import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; public class Test {   static final String LOGON_SITE = "http://mail.163.com";   static final int LOGON_PORT = 80;   public static void main(String[] args) throws Exception    {      HttpClient client = new HttpClient();      client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);     //登录      PostMethod post = new PostMethod(         "http://reg.163.com/logins.jsp?type=1&url=http://fm163.163.com/coremail/fcg/ntesdoor2?lightweight=1&verifycookie=1&language=-1&style=16");      NameValuePair username = new NameValuePair("username", "你的用户名@163.com");      NameValuePair password = new NameValuePair("password", "你的密码");      post.setRequestBody(new NameValuePair[] { username, password });      client.executeMethod(post);      String responseString = new String(post.getResponseBodyAsString().getBytes(         "gbk"));      System.out          .println("=========================登录页面===========================");      System.out.println(responseString);      Cookie[] cookies = client.getState().getCookies();      client.getState().addCookies(cookies);      post.releaseConnection();     int startPos = responseString          .indexOf("http://reg.youdao.com/crossdomain.jsp?username=");     int endPos = responseString.indexOf(""", startPos + 1);      String newUrl = responseString.substring(startPos, endPos);      System.out          .println("===========================第一次页面转向============================");      System.out.println(newUrl);     //以get方式请求跳转页面      GetMethod get = new GetMethod(newUrl);      get.setRequestHeader("Cookie", cookies.toString());      client.executeMethod(get);      responseString = new String(get.getResponseBodyAsString().getBytes("gbk"));     //向控制台打印登陆后页面的html      System.out          .println("==========================第一次转向后的页面==================================");      System.out.println(responseString);      get.releaseConnection();      startPos = responseString.indexOf("http://fm163.163");      endPos = responseString.indexOf(""", startPos + 1);      newUrl = responseString.substring(startPos, endPos);      System.out          .println("=============================第二次页面转向=======================================");      System.out.println(newUrl);      get = new GetMethod(newUrl);      get.setRequestHeader("Cookie", cookies.toString());      client.executeMethod(get);      responseString = new String(get.getResponseBodyAsString().getBytes("gbk"));      System.out.println("==========================第二次转向后的页面=============================");      System.out.println(responseString);      get.releaseConnection();    } } 
?

文章出处:http://xieruilin.iteye.com/blog/746299

热点排行