我正在創建需要連接到基於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社區;希望你能幫助。
並不令人意外。 http POST通過消息正文傳遞表單數據。 URL中的查詢參數成爲請求標題的一部分。除非特意告訴服務器端腳本查看主體和標題,否則標題數據將永遠不會顯示,因爲在執行POST時不希望通過url傳遞數據。 – 2013-03-15 20:16:05
因此,如果我想要實現這樣的功能,我需要來自該網站的開發人員支持? – 2013-03-15 20:56:23
我確定你可以在沒有網站開發人員的支持下登錄。 用jsoup嘗試 – MariuszS 2013-03-15 21:50:20