最近我試圖讓應用程序關注特定的網站。我需要訪問登錄後具有可見內容的頁面。如果我明白,下面的代碼顯示,首先我需要連接到url1以避免默認主頁,所以在這裏我無法發送數據(登錄名,密碼)。我需要看到來自url3的內容,但在這裏我也無法發送數據,因爲沒有登錄名和密碼字段。他們在url2中。我嘗試了本網站的其他解決方案,但我只收到每個人都可以看到的內容。任何人都可以幫忙嗎?發送數據需要查看來自網站的隱藏內容
private class Parser extends AsyncTask<Void, Void, Void> {
String h;
String url1 = "http://www.klt.net.pl/";
String url2 = "http://www.klt.net.pl/index.php?a=logowanie";
String url3 = "http://www.klt.net.pl/index.php?a=przedmecz1&b=2&d=2038";
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(MainActivity.this);
pd.setTitle("Parser");
pd.setMessage("Loading...");
pd.setIndeterminate(false);
pd.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
Connection.Response response = Jsoup.connect(url1)
.method(Connection.Method.GET)
.timeout(50000)
.followRedirects(true)
.execute();
Document document = Jsoup.connect(url2)
.cookies(response.cookies())
.get();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
pd.dismiss();
}
}
編輯:
@Override
protected Void doInBackground(Void... params) {
try {
Connection.Response response = Jsoup.connect(url1)
.method(Connection.Method.GET)
.timeout(50000)
.followRedirects(true)
.execute();
Connection.Response loginRes = Jsoup.connect(url2)
.userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36")
.data("login", getlog2,
"haslo", getpass2)
.cookies(response.cookies())
.method(Method.POST)
.execute();
Map<String, String> cookies = new Map<String, String>();
cookies.addAll(loginRes.cookies());
Connection.Response otherRes = Jsoup.connect(url3)
.cookies(cookies)
.method(Method.POST)
.execute();
d3 = Jsoup.connect(url3)
.cookies(otherRes.cookies())
.get();
我更新的代碼。這可以嗎?我在Map中有錯誤(無法安裝類型並且無法解析類型)。
你的問題還不是很清楚 –
這可以幫助更好地理解:http://stackoverflow.com/questions/32734928/jsoup-parsing-from-direct-link-doesnt-work/32737661?noredirect = 1#comment53330262_32737661但現在我需要從url3獲取內容。 – zdc
嘿:)這裏可以幫助你解決第二個問題!所以你想通過url2登錄來訪問url3中的內容,這是正確的嗎? –