简体   繁体   English

使用 httpurlconnection 登录 Facebook

[英]Log in to facebook using httpurlconnection

I'm trying to login to facebook using httpurlconnect.我正在尝试使用 httpurlconnect 登录 Facebook。 I think i need to get the cookie from facebook.com/ajax/bz and then pass it along with my login attempt but maybe not.我想我需要从facebook.com/ajax/bz获取 cookie,然后将它与我的登录尝试一起传递,但也许不是。 The response just takes me back to the login page and I never log in. Can someone help me get this to log in?响应只是将我带回登录页面,而我从未登录。有人可以帮助我登录吗?

I do not want to use the official API.我不想使用官方 API。

private String getRequestCookie() throws IOException {
    // URL myUrl = new URL("http://www.hccp.org/cookieTest.jsp");
  /*  URLConnection urlConn = null;
    try {
        urlConn = url.openConnection(currentProxy);
        urlConn.connect();
    } catch (IOException e) {
        e.printStackTrace();
    }*/

    String cookies = "";
    URL myUrl = new URL("https://www.facebook.com/ajax/bz");
    URLConnection urlConn = myUrl.openConnection();
    urlConn.connect();
    String headerName=null;
    for (int i=1; (headerName = urlConn.getHeaderFieldKey(i))!=null; i++) {
        if (headerName.equals("Set-Cookie")) {
            String cookie = urlConn.getHeaderField(i);
            cookie = cookie.substring(0, cookie.indexOf(";"));
            String cookieName = cookie.substring(0, cookie.indexOf("="));
            String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length());
            cookies = cookies+cookieName+"="+cookieValue+"; ";
            //cookieNameList.add(cookieValue);
            System.out.println("da cookies:" + cookies);

            return cookies;
        }
    }
    // must have failed yo
    cookies = "failed";
    return cookies;
}
private String performJsonPost() {
    try {
        String cookies = getRequestCookie();
        String query = "email=EMAILNAME%40EMAIL.com&pass=PASSWORD&lsd=AVrVVnf1&default_persistent=0&timezone=&lgnrnd=132329_hCSS&lgnjs=n&locale=tr_TR";
        URL urls = new URL("https://www.facebook.com/login.php?login_attempt=1");

        final HttpURLConnection conn = (HttpURLConnection)urls.openConnection();
        final byte[] payloadAsBytes = query.getBytes(Charset.forName("UTF-8"));
        conn.setConnectTimeout(15000);
        conn.setReadTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Cookie", "reg_fb_gate=https%3A%2F%2Fwww.facebook.com%2F; reg_fb_ref=https%3A%2F%2Fwww.facebook.com%2F; reg_ext_ref=deleted; Max-Age=0; datr=0aFzVK8Fu0Gl7M8cn_6TSqlZ");
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", "" + payloadAsBytes.length);
        conn.setRequestProperty("Content-Language", "en-US");
        conn.setUseCaches(false);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        final DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
        outStream.write(payloadAsBytes);
        //   outStream.write(payloadAsBytes);
        outStream.flush();
        outStream.close();

        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;

      /*  catch (Exception e2) {
            //inStream = conn.getErrorStream();
        }*/
            final StringBuilder response = new StringBuilder();
            final byte[] buffer = new byte[1024];
            int bytesRead;
            //System.out.println(inStream.toString() + "WHAT ARE U SAYING BRO");
            //while ((bytesRead = inStream.read(buffer)) > 0) {
            //response.append(new String(buffer, "UTF-8").substring(0, bytesRead));
            while ((line = rd.readLine()) != null) {
                System.out.println(line);
                response.append(line);
                //  inStream = conn.getInputStream();
            }
            return response.toString();
        }catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    //    System.out.println("Response!:"+response.toString());
      //  return response.toString();
}
    catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

It doesn't matter that you want to do it;你想做什么并不重要; according to the Automated Data Collection Terms , it's not allowed.根据自动数据收集条款,这是不允许的。 You app/server can be blocked.您的应用程序/服务器可以被阻止。 Don't do this.不要这样做。 Instead, just use the Graph API and all documented features.相反,只需使用Graph API和所有记录的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM