我的代码如下:
public class FormLoginDemo { 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", "用户名");
NameValuePair password = new NameValuePair("password", "密码");
post.setRequestBody(new NameValuePair[]{username,password});
client.executeMethod(post);
Cookie[] cookies=client.getState().getCookies();
client.getState().addCookies(cookies);
post.releaseConnection();
//访问登陆后的页面
Header header = post.getResponseHeader("Location");
String newUrl = header.getValue(); //获得跳转页面地址
System.out.println(newUrl);
String urlx=null;
//以get方式请求跳转页面
GetMethod get = new GetMethod(newUrl);
get.setRequestHeader("Cookie",cookies.toString());
client.executeMethod(get);
//向控制台打印登陆后页面的html
System.out.println(get.getResponseBodyAsString());
get.releaseConnection();
}
}总是登陆到 跳转提示页面 提示说:网速慢或系统拒绝跳转
public class FormLoginDemo { 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", "用户名");
NameValuePair password = new NameValuePair("password", "密码");
post.setRequestBody(new NameValuePair[]{username,password});
client.executeMethod(post);
Cookie[] cookies=client.getState().getCookies();
client.getState().addCookies(cookies);
post.releaseConnection();
//访问登陆后的页面
Header header = post.getResponseHeader("Location");
String newUrl = header.getValue(); //获得跳转页面地址
System.out.println(newUrl);
String urlx=null;
//以get方式请求跳转页面
GetMethod get = new GetMethod(newUrl);
get.setRequestHeader("Cookie",cookies.toString());
client.executeMethod(get);
//向控制台打印登陆后页面的html
System.out.println(get.getResponseBodyAsString());
get.releaseConnection();
}
}总是登陆到 跳转提示页面 提示说:网速慢或系统拒绝跳转
解决方案 »
- 求一正则、自己真不会正则
- JSp页面显示当前月有几周每一周从几号到几号
- 求解决方法
- 数组问题
- 初学java 怎样把servlet,jsp,javabean之类的组合起来完成系统开发
- 请问谁有Myeclipse for linux软件下载啊
- Hibernate读取数据不一致
- 我把jbuilder和weblogic连起来了,作了一个最简单的servlet。部署也成功了(起码没包错)不过我访问时 http://localhost:7001/firstWebAp
- EJB 中 Java基础的问题。
- 我装完tomcat4.1后,http://localhost:8080/页面只能访问一次,关了之后就访问不了了,怎么回事?
- 用get方式提交如何取到某个元素的值
- jsp页面无法获取action参数
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;/**
*
*//**
* <p><b>Class: </b>.FormLoginDemo</p>
*
* <p><b>Create Date: </b>Jan 4, 2009</p>
*
* @author Yang Yang
*
*/
public class FormLoginDemo
{ 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();
}
}
******************************第二次转向后的页面******************************
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<link rel="shortcut icon" href="http://p.mail.163.com/images/favicon.ico" />
<title>网易电子邮箱 - 极速3.0</title> </HEAD>
<BODY style="margin:0;padding:0;" scroll="no">
<iframe src="index.jsp?sid=xBLXwdvvyyVIJyiEeTvvqOhmXDbOUrnr" name="index" style="width:100%;height:100%" frameborder="0" border="0"></iframe>
</BODY>
</HTML>
我想要的是邮箱主页面,这应该是框架把。还需1楼的帮忙。
100分在等着你哦 呵呵
我也获得了那个iframe的,下面就访问不了。直接访问SRC那个路径,打印出来的页面代码是要输入验证码的,说是防止程序自动登录
用foxmail 能设置客户端成功就可以,不可以就不可以
******************************第一次页面转向******************************
http://reg.youdao.com/crossdomain.jsp?username=testhttp&loginCookie=fZ_v5WyPqd5ffLPCwYXR7o3flIcFhWehHx8HYcfLted7eJ2XDeA2YGqI_UbylXbgwsrMx6y1cdMebCiZjNB.i2MfGGbHgkuLq&sInfoCookie=1240025609|0&pInfoCookie=testhttp%40163.com%7C1240025609%7C0%7Cother%7C10%2618%261240025609198&url=http%3A%2F%2Ffm163.163.com%2Fcoremail%2Ffcg%2Fntesdoor2%3Flightweight%3D1%26username%3Dtesthttp
Exception in thread "main" java.lang.IllegalArgumentException: Invalid uri 'http://reg.youdao.com/crossdomain.jsp?username=testhttp&loginCookie=fZ_v5WyPqd5ffLPCwYXR7o3flIcFhWehHx8HYcfLted7eJ2XDeA2YGqI_UbylXbgwsrMx6y1cdMebCiZjNB.i2MfGGbHgkuLq&sInfoCookie=1240025609|0&pInfoCookie=testhttp%40163.com%7C1240025609%7C0%7Cother%7C10%2618%261240025609198&url=http%3A%2F%2Ffm163.163.com%2Fcoremail%2Ffcg%2Fntesdoor2%3Flightweight%3D1%26username%3Dtesthttp': Invalid query
at org.apache.commons.httpclient.HttpMethodBase.<init>(HttpMethodBase.java:222)
at org.apache.commons.httpclient.methods.GetMethod.<init>(GetMethod.java:89)
at test.Test4.main(Test4.java:58)
Exception in thread "main" java.lang.IllegalArgumentException: Invalid uri 'http://reg.youdao.com/crossdomain.jsp?userna
me=103752474&loginCookie=wOOuYP5F6ufLmFMdw.jzYT8xb8Z7R1ZY.oOpIjPPY7gB6H1js6r1gXCHXnqGrCgEDR5WhsroNuD.nDxJQ5_OYEswXXFof90
Bz&sInfoCookie=1248402451|0&pInfoCookie=103752474%40163.com%7C1248402451%7C0%7Cother%7C10%2619%261248402451240&url=http%
3A%2F%2Ffm163.163.com%2Fcoremail%2Ffcg%2Fntesdoor2%3Flightweight%3D1%26username%3D103752474': Invalid query
at org.apache.commons.httpclient.HttpMethodBase.<init>(HttpMethodBase.java:222)
at org.apache.commons.httpclient.methods.GetMethod.<init>(GetMethod.java:89)
at Test.main(Test.java:45)
你用以上的方式可以获得到iframe页面吗