0

我第一次尝试登录 http 网站,我很难理解发送参数的正确格式。我查看了其他示例,它们似乎对我不起作用,所以我想我会看看是否有人可以向我解释这一点。在这一点上,我的代码似乎什么也没做,但它在这里......

HttpURLConnection url= (HttpURLConnection)new URL("http://www.myameego.com/index2.php?do=login").openConnection();
url.setDoOutput(true);
url.setRequestMethod("POST");
OutputStreamWriter writer = new OutputStreamWriter(url.getOutputStream());
writer.write("X-Mapping-fjhppofk=6A991610BA398B3A39F4B491D5382BB4;
PHPSESSID=kbo25e08t3qvu08l1shkq8kk94; userName=coled; pass=ed45d626b07112a8a501d9672f3b92796a6754b8d8d9cb4c617fec9774889220; clientID=129; X-Mapping-fjhppofk=DCE62FE972E1EF2F12D0060EC74C3681; PHPSESSID=ukeo21oldb5pqsntu7kl8j3b96");
writer.flush();

我下载了一个 http 嗅探器,认为我可以读取浏览器发送的内容。这就是我得到 write() 行的方式,它是资源管理器发送的 cookie。我还查看了登录屏幕的源代码,并在底部附近发现了一段看起来像是负责登录的代码。

http://www.myameego.com/index2.php?do=login

有人能告诉我如何连接到这个界面吗?我不明白它是如何工作的。如果有帮助,这是我通过浏览器手动登录的完整数据包。我从我的 http 嗅探器中得到它。

Host Name: www.myameego.com
Method: POST
Path: /index2.php?do=login
User Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0; NP06)
Response Code: 302
Response String: found
Content Type: text/html; charset=UTF-8
Referer: http://www.myameego.com/index.php?do=login
Transfer Encoding: chunked
Server: Apache
Content Length: 17817
Connection: Keep-Alive
Cache Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Location: /Ameego/index.php
Cookie: X-Mapping-fjhppofk=6A991610BA398B3A39F4B491D5382BB4; PHPSESSID=kbo25e08t3qvu08l1shkq8kk94; userName=coled; pass=ed45d626b07112a8a501d9672f3b92796a6754b8d8d9cb4c617fec9774889220; clientID=129; X-Mapping-fjhppofk=DCE62FE972E1EF2F12D0060EC74C3681; PHPSESSID=ukeo21oldb5pqsntu7kl8j3b96
URL: http://www.myameego.com/index2.php?do=login

我怎样才能制作一个像上面这样的包?任何指导将不胜感激。

我查看了您发布的链接,http 嗅探器显示正在调用 POST 请求,但 cookie 行与手动浏览器请求的不匹配。

HttpURLConnection httpConnection = (HttpURLConnection)new URL("http://www.myameego.com/index2.php?do=login").openConnection();
    httpConnection.setDoOutput(true);
    httpConnection.setRequestMethod("POST");
    httpConnection.setRequestProperty("Accept-Charset","UTF-8");
    httpConnection.setRequestProperty("User-Agent","Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0; NP06)");
    httpConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
    String info = String.format("user=%s&coled=%s",URLEncoder.encode("user","UTF-8"),URLEncoder.encode("coled","UTF-8"));
    info += String.format("pass=%s&MYPASS=%s",URLEncoder.encode("pass","UTF-8"),URLEncoder.encode("MYPASS","UTF-8"));
    info += String.format("clientID=%s&129=%s",URLEncoder.encode("clientID","UTF-8"),URLEncoder.encode("129","UTF-8"));
    info += String.format("login=%s&Sign In=%s",URLEncoder.encode("login","UTF-8"),URLEncoder.encode("Sign In","UTF-8"));
    httpConnection.setRequestProperty("Cookie",info);
    OutputStream output = httpConnection.getOutputStream();
    output.write(info.getBytes("UTF-8"));
    int x;
    while((x = httpConnection.getInputStream().read()) != -1)System.out.print((char)x);

我的 Cookie:user=user&coled=coledpass=pass&MYPASS=MYPASSclientID=clientID&129=129login=login&Sign In=Sign+In

浏览器cookie:
X-Mapping-fjhppofk=6A991610BA398B3A39F4B491D5382BB4;PHPSESSID=112tg9i4afau5i382hui705553

有谁知道我在这里可能缺少什么?

4

1 回答 1

1

使用 Jsoup 这应该很简单,如下所示:

Connection.Response response = Jsoup.connect("http://www.myameego.com/index2.php?do=login")
        .method(Connection.Method.GET)
        .execute();

Document page = Jsoup.connect("http://www.myameego.com/index2.php?do=login")
        .data("user", "login")
        .data("pass", "password")
        .data("clientID", "123456")
        .cookies(response.cookies())
        .post();

与Google Chrome 开发者工具一起收集

要求

使用的 cookie

于 2013-05-05T11:07:16.977 回答