使用Java登录www.messenger.com

时间:2016-06-19 19:54:43

标签: java parameter-passing urlconnection

我无法使用我发现登录www.messenger.com的代码。好像我无法写出表单参数,因为我没有正确的表单名称。我无法找到按钮的表单名称以及将其设置为等于的内容。我的最终目标是在登录后获取HTML代码。

来源:http://www.dreamincode.net/forums/blog/114/entry-2715-login-to-a-website-from-java/

import java.net.*;
import java.io.*;

private static URL URLObj;
private static URLConnection connect;

public static void main(String[] args) {
    try {
        // Establish a URL and open a connection to it. Set it to output mode.
        URLObj = new URL("http://www.messenger.com/#");
        connect = URLObj.openConnection();
        connect.setDoOutput(true);  
    }
    catch (MalformedURLException ex) {
        System.out.println("The URL specified was unable to be parsed or uses an invalid protocol. Please try again.");
        System.exit(1); 
    }
    catch (Exception ex) {
        System.out.println("An exception occurred. " + ex.getMessage());
        System.exit(1);
    }


    try {
        // Create a buffered writer to the URLConnection's output stream and write our forms parameters.
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connect.getOutputStream()));
        writer.write("email=MyEmail&pass=MyPassword&submit=Sign In");
        //writer.close();

        // Now establish a buffered reader to read the URLConnection's input stream.
        BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));

        String lineRead = "";

        // Read all available lines of data from the URL and print them to screen.
        while ((lineRead = reader.readLine()) != null) {
            System.out.println(lineRead);
        }
        reader.close();
    }
    catch (Exception ex) {
        System.out.println("There was an error reading or writing to the URL: " + ex.getMessage());
    }
}

1 个答案:

答案 0 :(得分:0)

当您单击login:

时,这是由浏览器发送的帖子参数
default_persistent=0
email=user
initial_request_id=A2NPA_SLbM3wAkFRM_Y0fLx
lgndim=eyJ3IjoxOTIwLCJoIjoxMjAwLCJhdyI6MTkyMCwiYWgiOjExNjAsImMiOjI0fQ==
lgnjs=n
lgnrnd=125813_Br9w
login=1
lsd=AVrsF9i0
pass=pass
timezone=-120

也许您需要其中一些才能成功登录。

您可以在表单中找到这些隐藏参数,其中包含id" login_form"。