我的代码如下:
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();
    }
}总是登陆到   跳转提示页面 提示说:网速慢或系统拒绝跳转

解决方案 »

  1.   

    我替你改了一个,试过好用。
    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();
      }
    }
      

  2.   

    运行后得到这个页面
    ******************************第二次转向后的页面******************************
    <!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分在等着你哦  呵呵
      

  3.   

    我也想知道如何得到此IFRAME中的内容,我试了之后,好像转到了登录页面?
      

  4.   

    怎么顶不起来了
    我也获得了那个iframe的,下面就访问不了。直接访问SRC那个路径,打印出来的页面代码是要输入验证码的,说是防止程序自动登录
      

  5.   

    有可能是你的邮箱不支持客户端首发信件!
    用foxmail 能设置客户端成功就可以,不可以就不可以
      

  6.   

    请问为什么我用2楼代码的时候会报错:
    ******************************第一次页面转向******************************
    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)
      

  7.   

    同上楼那个,怎么解决这个问题呢
    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)
      

  8.   

    只获得iframe 这个页面,后面怎么解决的?求高人指点。。
      

  9.   

    17楼
    你用以上的方式可以获得到iframe页面吗