2013-05-04 238 views
0

我想第一次登錄到http網站,我很難理解發送參數的正確格式。我看了其他例子,他們似乎並沒有爲我工作,所以我想我會看看是否有人可以向我解釋這一點。此時我的代碼似乎什麼都不做,但在這裏它是...登錄到http網站java

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()行,它是由explorer發送的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 sniffer顯示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: 用戶=用戶& COLED = coledpass =傳遞&爲mypass = MYPASSclientID = clientID的& 129 = 129login =登錄&登錄=符號+在

瀏覽器使用網站:
X-映射-fjhppofk = 6A991610BA398B3A39F4B491D5382BB4; PHPSESSID = 112tg9i4afau5i382hui705553

有人知道我可能會在這裏失蹤嗎?

+0

雲集請後,你可能會跨越未來的任何錯誤,所以會更容易理解問題 – Chakri 2013-05-04 09:31:01

+0

爲什麼你不試圖轉儲誰請求包括所有的HTTP頭(服務器可能會檢查這個)?順便說一句 - 你允許這樣做嗎?你確定你可以重複使用所有這些神奇的數字嗎?任何想法如何HTTP的作品(你的流出沒有意義)? – home 2013-05-04 09:37:17

+0

在看起來很有幫助之前,我還沒有看到過這個鏈接。我之前的代碼甚至沒有發送一個包謝謝。我不知道如何獲得PHPSESSID或X-Mapping的所有內容 – user2349274 2013-05-05 10:28:41

回答

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 Developer Tools

request

cookies used

+0

您如何查看網頁? – user2349274 2013-05-06 00:40:54

+0

它給我發送一份我認爲的要求清單? 我知道這個網站在FlashPlayer上運行,這對我來說會是一個問題嗎? – user2349274 2013-05-06 01:10:16

+0

哦,我得到它的工作。它給了我緊接着post()調用之後的網頁上的選項列表。如何在此之後撥打電話?\ – user2349274 2013-05-06 01:28:22