HttpClient 重定向句柄和 facebook 登录(无需浏览器)的问题

发布于 2024-10-01 07:44:46 字数 4804 浏览 2 评论 0原文

我花了三周的时间找到了一个无需浏览器即可登录facebook home的解决方案。我编写了这个示例代码,但它返回一个错误:

org.apache.commons.httpclient.HttpMethodDirector isRedirectNeeded 信息:已请求重定向,但 followRedirects 已禁用

代码:

import java.io.IOException;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;

public class formLogin {

    public static void main(String[] args) throws HttpException, IOException {
        // Set target URL
        String strURL = "http://www.facebook.com/login.php";;
        System.out.println("Target URL: " + strURL);

        // Get initial state object
        HttpState initialState = new HttpState();

        // Initial set of cookies can be retrieved from persistent storage
        // and re-created, using a persistence mechanism of choice,
        //Cookie mycookie = new Cookie(".foobar.com", "mycookie", "stuff",
        //        "/", null, false);

        // and then added to your HTTP state instance
        //initialState.addCookie(mycookie);

        // Get HTTP client instance
        HttpClient httpclient = new HttpClient();
        httpclient.getHttpConnectionManager().
                getParams().setConnectionTimeout(30000);

        // do not set preemptive, this forces Basic authentication which is not what we want
        httpclient.getHostConfiguration().getParams()
            .setParameter(HttpClientParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

        httpclient.setState(initialState);

        // RFC 2101 cookie management spec is used per default
        // to parse, validate, format & match cookies
        //httpclient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);

        // A different cookie management spec can be selected
        // when desired
        //httpclient.getParams().setCookiePolicy(CookiePolicy.NETSCAPE);

        // Netscape Cookie Draft spec is provided for completeness
        // You would hardly want to use this spec in real life situations
        httpclient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

        // Compatibility policy is provided in order to mimic cookie
        // management of popular web browsers that is in some areas
        // not 100% standards compliant

        // Get HTTP GET method    
        GetMethod httpget = new GetMethod(strURL);   

        // Execute HTTP GET
        int result = httpclient.executeMethod(httpget);

        // Display status code
        System.out.println("Response status code: " + result);

        // Get all the cookies
        Cookie[] cookies = httpclient.getState().getCookies();

        // Display the cookies
        System.out.println("Present cookies: ");
        for (int i = 0; i < cookies.length; i++) {
            System.out.println(" - " + cookies[i].toExternalForm());
        }

        // Release current connection to the connection pool
        // once you are done
        httpget.releaseConnection();

        //Cookie ist da und Jetzt wird eingeloggt

        PostMethod postMethod = new PostMethod("http://facebook.com/login.php");

        NameValuePair[] postData = new NameValuePair[2];
        postData[0] = new NameValuePair("email", "******");
        postData[1] = new NameValuePair("pass", "******");

        postMethod.setRequestBody(postData);
        for(int i = 0; i < cookies.length; i++){
            postMethod.setRequestHeader("Cookie:",cookies[i].toExternalForm());
        }

        try {
            httpclient.executeMethod(postMethod);
            int statuscode = postMethod.getStatusCode();
            System.out.println("STATUS CODE = "+statuscode);
        } catch (HttpException httpe) {
            System.err.print("HttpException");
            System.err.println(httpe.getMessage());
            httpe.printStackTrace();
        } catch (IOException ioe) {
            System.err.print("IOException");
            System.err.println(ioe.getMessage());
            ioe.printStackTrace();
        }
        String responseBody = postMethod.getResponseBodyAsString();
        System.out.println(responseBody);

        postMethod.releaseConnection();
    }
}

此代码正确吗?我需要做什么才能遵循重定向?我已经尝试使用 postmethod.setfollowredirect(true) 但错误仍然存​​在。

I spent three weeks to find a solution to login on facebook home without browser. I wrote this example code but it return an error:

org.apache.commons.httpclient.HttpMethodDirector isRedirectNeeded
INFO: Redirect requested but followRedirects is disabled

Code:

import java.io.IOException;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;

public class formLogin {

    public static void main(String[] args) throws HttpException, IOException {
        // Set target URL
        String strURL = "http://www.facebook.com/login.php";;
        System.out.println("Target URL: " + strURL);

        // Get initial state object
        HttpState initialState = new HttpState();

        // Initial set of cookies can be retrieved from persistent storage
        // and re-created, using a persistence mechanism of choice,
        //Cookie mycookie = new Cookie(".foobar.com", "mycookie", "stuff",
        //        "/", null, false);

        // and then added to your HTTP state instance
        //initialState.addCookie(mycookie);

        // Get HTTP client instance
        HttpClient httpclient = new HttpClient();
        httpclient.getHttpConnectionManager().
                getParams().setConnectionTimeout(30000);

        // do not set preemptive, this forces Basic authentication which is not what we want
        httpclient.getHostConfiguration().getParams()
            .setParameter(HttpClientParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

        httpclient.setState(initialState);

        // RFC 2101 cookie management spec is used per default
        // to parse, validate, format & match cookies
        //httpclient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);

        // A different cookie management spec can be selected
        // when desired
        //httpclient.getParams().setCookiePolicy(CookiePolicy.NETSCAPE);

        // Netscape Cookie Draft spec is provided for completeness
        // You would hardly want to use this spec in real life situations
        httpclient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

        // Compatibility policy is provided in order to mimic cookie
        // management of popular web browsers that is in some areas
        // not 100% standards compliant

        // Get HTTP GET method    
        GetMethod httpget = new GetMethod(strURL);   

        // Execute HTTP GET
        int result = httpclient.executeMethod(httpget);

        // Display status code
        System.out.println("Response status code: " + result);

        // Get all the cookies
        Cookie[] cookies = httpclient.getState().getCookies();

        // Display the cookies
        System.out.println("Present cookies: ");
        for (int i = 0; i < cookies.length; i++) {
            System.out.println(" - " + cookies[i].toExternalForm());
        }

        // Release current connection to the connection pool
        // once you are done
        httpget.releaseConnection();

        //Cookie ist da und Jetzt wird eingeloggt

        PostMethod postMethod = new PostMethod("http://facebook.com/login.php");

        NameValuePair[] postData = new NameValuePair[2];
        postData[0] = new NameValuePair("email", "******");
        postData[1] = new NameValuePair("pass", "******");

        postMethod.setRequestBody(postData);
        for(int i = 0; i < cookies.length; i++){
            postMethod.setRequestHeader("Cookie:",cookies[i].toExternalForm());
        }

        try {
            httpclient.executeMethod(postMethod);
            int statuscode = postMethod.getStatusCode();
            System.out.println("STATUS CODE = "+statuscode);
        } catch (HttpException httpe) {
            System.err.print("HttpException");
            System.err.println(httpe.getMessage());
            httpe.printStackTrace();
        } catch (IOException ioe) {
            System.err.print("IOException");
            System.err.println(ioe.getMessage());
            ioe.printStackTrace();
        }
        String responseBody = postMethod.getResponseBodyAsString();
        System.out.println(responseBody);

        postMethod.releaseConnection();
    }
}

Is this code correct? What do I have to do to follow a redirect? I've already tried with postmethod.setfollowredirect(true) but the error persists.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兮子 2024-10-08 07:44:46

PostMethod 是一个EntityEnclosureMethod。在 PostMethod 上调用 setFollowRedirect() 不起作用。请参阅:

http://www.w3.org/Protocols/rfc2616 /rfc2616-sec10.html#sec10.3

也许您应该首先发出 HEAD 请求,或者您可以从不同的 URL 开始。尝试使用“http://www.facebook.com/login.php”而不是您拥有的 URL。

PostMethod is an EntityEnclosingMethod. Calling setFollowRedirect() on PostMethod won't work. See:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3

Maybe you should make a HEAD request first, or maybe you could just start off with a different URL. Try "http://www.facebook.com/login.php" instead of the URL you have.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文