2013-03-15 45 views
1

我正在創建需要連接到基於php的站點並登錄以收集相關數據的第三方Java應用程序(桌面)。沒有可訪問的Web服務,沒有API,每個用戶都將擁有自己的安全登錄。該網站使用dojo(如果這很重要),我使用Java HttpClient發送帖子。登錄到基於php的網站和抓取數據 - 問題

HttpPost httppost = new HttpPost("https://thewebsite.net/index/login"); // .php ? 
// Create a new HttpClient and Post Header 
HttpClient httpclient = new DefaultHttpClient(); 

//initialize the response string  
String nextpage = ""; 

try { 
    // Add nvps 
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3); 
    nameValuePairs.add(new BasicNameValuePair("", "")); 
    nameValuePairs.add(new BasicNameValuePair("login", "USER")); 
    nameValuePairs.add(new BasicNameValuePair("", "")); 
    nameValuePairs.add(new BasicNameValuePair("pass", "PASSWORD")); 
    nameValuePairs.add(new BasicNameValuePair("Submit", "")); 

    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

HttpResponse response = httpclient.execute(httppost); 
userID = EntityUtils.toString(response.getEntity()); 

System.out.println(nextpage); 
httppost.releaseConnection(); 
} 
... 

現在,我遇到的問題是,給我的反應是通過道場的用戶名/密碼的字段驗證的JScript。

<script type='text/javascript'> 
dojo.require("dojox.validate._base"); 

function validate_RepeatPassword(val, constraints) 
{ 
    var isValid = false; 

    if(constraints) { 
     var otherInput = dijit.byId(constraints[0]); 
     if(otherInput) { 
     var otherValue = otherInput.value; 
      isValid = (val == otherValue); 
     } 
    } 
    return isValid; 
} 

</script> 

我只是想連接,解析一個html響應,並關閉連接。

當我使用Firebug,我得到這個爲POST方法,但我似乎無法得到它的運行: Referer的https://thewebsite.net/index/login 登錄源= USER &通= PASSWORD

當我使用HttpPost客戶端建立一個直接的URL後沒有namevaluepairs中:

HttpPost httppost = new HttpPost("https://thewebsite.net/index/login?login=USER&pass=PASSWORD"); 

,我得到一條錯誤反應「的用戶,並通過字段不能留空。」

我的問題是:是否有直接的方法登錄,更簡單,我錯過了,可以讓我成功繼續過去登錄?

謝謝 - 我喜歡SO社區;希望你能幫助。

+0

並不令人意外。 http POST通過消息正文傳遞表單數據。 URL中的查詢參數成爲請求標題的一部分。除非特意告訴服務器端腳本查看主體和標題,否則標題數據將永遠不會顯示,因爲在執行POST時不希望通過url傳遞數據。 – 2013-03-15 20:16:05

+0

因此,如果我想要實現這樣的功能,我需要來自該網站的開發人員支持? – 2013-03-15 20:56:23

+0

我確定你可以在沒有網站開發人員的支持下登錄。 用jsoup嘗試 – MariuszS 2013-03-15 21:50:20

回答

0

我沒有最終使用您的確切代碼(與發佈參數),但JSoup是修復。

這裏是我用什麼:

`res = Jsoup.connect("https://thewebsite.net/index/login") 
    .data("login", User).data("pass", Pass) 
    .userAgent("Chrome").method(Method.POST).execute(); 

//then I grabbed the cookie and sent the next post for the data 

Document t = res.parse(); //for later use 
SessionID = res.cookie("UNIQUE_NAME"); 

//the JSON 
Connection.Response driverx =  Jsoup.connect("https://thewebsite.net/datarequest/data").cookie("UNIQUE_NAME",SessionID).userAgent("Chrome").method(Method.POST).execute();` 
0

我覺得這樣做最好的圖書館是jsoup

Connection.Response res = 
Jsoup.connect("https://thewebsite.net/index/login?login=USER&pass=PASSWORD") 
.method(Method.POST) 
.execute(); 

爲此,你需要做驗證後也。你需要讀取cookie,請求參數和頭參數,這將工作。

+0

我沒有結束使用您的確切代碼(使用post參數),但JSoup是修復程序。 – 2013-03-20 03:05:54