使用 Java 登录 www.messenger.com
Posted
技术标签:
【中文标题】使用 Java 登录 www.messenger.com【英文标题】:Using Java to login into www.messenger.com 【发布时间】:2016-10-21 01:02:17 【问题描述】:我在使用我发现的登录 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】:这是点击登录时由浏览器发送的帖子参数:
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”的表单中找到这些隐藏参数。
【讨论】:
以上是关于使用 Java 登录 www.messenger.com的主要内容,如果未能解决你的问题,请参考以下文章
用java设计一个“掌上微博”的用户登录界面,要求界面友好,使用方便,如下图