最近我试图让应用程序关注特定的网站。我需要访问登录后具有可见内容的页面。如果我明白,下面的代码显示,首先我需要连接到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中的内容,这是正确的吗? –