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();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值