2010-06-18 65 views
0

我試圖訪問一個ASPX網站,後續頁面根據 發佈數據返回。不幸的是,我所有嘗試獲取以下頁面都失敗了。 希望這裏有人有一個想法在哪裏找到錯誤!使用POST數據和Cookie處理網站

在第一步中,我從cookie中讀取會話ID以及返回的html頁面中的 viewstate變量的值。第二步打算將它發回 回到服務器以獲得所需的頁面。

嗅探在網頁瀏覽器中的數據給出了

Host=www.geocaching.com 
User-Agent=Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.9) Gecko/20100618 
Iceweasel/3.5.9 (like Firefox/3.5.9) 
Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language=en-us,en;q=0.5 
Accept-Encoding=gzip,deflate 
Accept-Charset=ISO-8859-1,utf-8;q=0.7,*;q=0.7 
Keep-Alive=300 
Connection=keep-alive 
Referer=http://www.geocaching.com/seek/nearest.aspx?state_id=149 
Cookie=Send2GPS=garmin; BMItemsPerPage=200; maprefreshlock=true; ASP. 
NET_SessionId=c4jgygfvu1e4ft55dqjapj45 
Content-Type=application/x-www-form-urlencoded 
Content-Length=4099 
POSTDATA=__EVENTTARGET=ctl00%24ContentBody%24pgrBottom% 
24lbGoToPage_3&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPD[...]2Xg%3D% 
3D&language=on&logcount=on&gpx=on 

目前,我的劇本是這樣的

import java.net.*; 
import java.io.*; 
import java.util.*; 
import java.security.*; 
import java.net.*; 

public class test1 { 
    public static void main(String args[]) { 
     // String loginWebsite="http://geocaching.com/login/default.aspx"; 
     final String loginWebsite = "http://www.geocaching.com/seek/nearest.aspx?state_id=159"; 
     final String POST_CONTENT_TYPE = "application/x-www-form-urlencoded"; 

     // step 1: get session ID from cookie 
     String sessionId = ""; 
     String viewstate = ""; 
     try { 
      URL url = new URL(loginWebsite); 

      String key = ""; 
      URLConnection urlConnection = url.openConnection(); 

      if (urlConnection != null) { 
       for (int i = 1; (key = urlConnection.getHeaderFieldKey(i)) != null; i++) { 
        // get ASP.NET_SessionId from cookie 
        // System.out.println(urlConnection.getHeaderField(key)); 
        if (key.equalsIgnoreCase("set-cookie")) { 
         sessionId = urlConnection.getHeaderField(key); 
         sessionId = sessionId.substring(0, sessionId.indexOf(";")); 

        } 
       } 

       BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); 

       // get the viewstate parameter 
       String aLine; 
       while ((aLine = in.readLine()) != null) { 
        // System.out.println(aLine); 
        if (aLine.lastIndexOf("id=\"__VIEWSTATE\"") > 0) { 
         viewstate = aLine.substring(aLine.lastIndexOf("value=\"") + 7, aLine.lastIndexOf("\" ")); 
        } 
       } 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     System.out.println(sessionId); 
     System.out.println("\n"); 
     System.out.println(viewstate); 
     System.out.println("\n"); 

     // String goToPage="3"; 

     // step2: post data to site 
     StringBuilder htmlResult = new StringBuilder(); 
     try { 

      String encoded = "__EVENTTARGET=ctl00$ContentBody$pgrBottom$lbGoToPage_3" + "&" + "__EVENTARGUMENT=" + "&" 
       + "__VIEWSTATE=" + viewstate; 

      URL url = new URL(loginWebsite); 
      URLConnection urlConnection = url.openConnection(); 
      urlConnection = url.openConnection(); 

      // Specifying that we intend to use this connection for input 
      urlConnection.setDoInput(true); 

      // Specifying that we intend to use this connection for output 
      urlConnection.setDoOutput(true); 

      // Specifying the content type of our post 
      urlConnection.setRequestProperty("Content-Type", POST_CONTENT_TYPE); 

      // urlConnection.setRequestMethod("POST"); 

      urlConnection.setRequestProperty("Cookie", sessionId); 
      urlConnection.setRequestProperty("Content-Type", "text/html"); 

      DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream()); 
      out.writeBytes(encoded); 
      out.flush(); 
      out.close(); 

      BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); 

      String aLine; 
      while ((aLine = in.readLine()) != null) { 
       System.out.println(aLine); 
      } 

     } catch (MalformedURLException e) { 
      // Print out the exception that occurred 
      System.err.println("Invalid URL " + e.getMessage()); 
     } catch (IOException e) { 
      // Print out the exception that occurred 
      System.err.println("Unable to execute " + e.getMessage()); 
     } 
    } 
} 

任何想法有什麼不對?任何幫助非常感謝!

更新

謝謝你的快速回復!

我切換到使用HttpURLConnection而不是實現setRequestMethod()的URLConnection。我還糾正了你提到的小錯誤,例如刪除了過時的第一個setRequestProperty調用。

不幸的是,這並沒有改變任何東西......我想我設置了所有相關的參數,但仍然只獲得列表的第一頁。看起來「__EVENTTARGET = ctl00 $ ContentBody $ pgrBottom $ lbGoToPage_3」被忽略。我沒有任何線索爲什麼。

內部,該網站上的形式如下:

<form name="aspnetForm" method="post" action="nearest.aspx?state_id=159" id="aspnetForm">

它是由下面的JavaScript調用:

<script type="text/javascript"> 
//<![CDATA[ 
var theForm = document.forms['aspnetForm']; 
if (!theForm) { 
    theForm = document.aspnetForm; 
} 
function __doPostBack(eventTarget, eventArgument) { 
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) { 
     theForm.__EVENTTARGET.value = eventTarget; 
     theForm.__EVENTARGUMENT.value = eventArgument; 
     theForm.submit(); 
    } 
} 
//]]> 
</script> 

希望,這有助於找到解決的辦法?

Greetings maik。

回答

0

你真的想要GET還是POST嗎?如果你想開機自檢,那麼你可能需要setRequestMethd()行。

您正在設置Content-Type兩次 - 我認爲您可能需要將它們合併爲一行。

然後,在嘗試從輸入流讀取之前,請不要關閉輸出流。

除此之外,還有沒有更多日誌可以放入/你可以提供哪些方式的錯誤?

0

嘿使用以下代碼

字符串的userAgent = 「的Mozilla/5.0(Windows NT的6.1; Win64的; 64; RV:25.0)壁虎/ 20100101火狐/ 25.0」;

 org.jsoup.nodes.Document jsoupDoc = Jsoup.connect(url).timeout(15000).userAgent(userAgent).referrer("http://calendar.legis.ga.gov/Calendar/?chamber=House").ignoreContentType(true) 
       .data("__EVENTTARGET", eventtarget).data("__EVENTARGUMENT", eventarg).data("__VIEWSTATE", viewState).data("__VIEWSTATEGENERATOR", viewStateGenarator) 
       .data("__EVENTVALIDATION", viewStateValidation).parser(Parser.xmlParser()).post();