我尝试使用JSoup登录网站,但我遇到了这个问题。 我知道登录后网站将我重定向到另一个页面。但我想使用一个网址
我不确定网址或登录数据是否不正确。
登录页面在这里
我目前正在尝试使用以下代码:
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
try {
String urlLogin = "http://www.ime.org.ir/members/entrancecheck.aspx";
String userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36";
Connection.Response response = Jsoup.connect(urlLogin)
.method(Connection.Method.GET)
.execute();
Document loginPage = response.parse();
response = Jsoup.connect(urlLogin)
.data("_LASTFOCUS", "")
.data("_EVENTTARGET", "")
.data("_EVENTARGUMENT","")
.data("__VIEWSTATE", loginPage.getElementById("__VIEWSTATE").val())
.data("__PREVIOUSPAGE", loginPage.getElementById("__PREVIOUSPAGE").val())
.data("__EVENTVALIDATION", loginPage.getElementById("__EVENTVALIDATION").val())
.data("ctl00$ContentPlaceHolder$entrancecheck1$username", "21465")
.data("ctl00$ContentPlaceHolder$entrancecheck1$pass", "amir1365")
.data("ctl00$ContentPlaceHolder$entrancecheck1$Button1", "ورود به پايگاه")
.userAgent(userAgent)
.followRedirects(true)
.cookies(response.cookies())
.method(Connection.Method.POST)
.execute();
Document doc = response.parse();
Elements description = doc
.select("table[id=table2]");
// Locate the content attribute
desc = description.html();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Set description into TextView
web= (WebView)findViewById(R.id.webView);
web.loadData(desc, "text/html; charset=UTF-8", null);
web.getSettings().setDefaultFontSize(15);
web.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
mProgressDialog.dismiss();
}
}
答案 0 :(得分:0)
您可以避免遵循重定向并检查响应标头是否包含Location
标头。
response = Jsoup.connect(urlLogin)
.data("__LASTFOCUS", "")
.data("__EVENTTARGET", "")
.data("__EVENTARGUMENT","")
.data("__VIEWSTATE", loginPage.getElementById("__VIEWSTATE").val())
.data("__PREVIOUSPAGE", loginPage.getElementById("__PREVIOUSPAGE").val())
.data("__EVENTVALIDATION", loginPage.getElementById("__EVENTVALIDATION").val())
.data("ctl00$ContentPlaceHolder$entrancecheck1$username", "21465")
.data("ctl00$ContentPlaceHolder$entrancecheck1$pass", "amir1365")
.data("ctl00$ContentPlaceHolder$entrancecheck1$Button1", "ورود به پايگاه")
.userAgent(userAgent)
.followRedirects(false)
.cookies(response.cookies())
.method(Connection.Method.POST)
.timeout(0)
.execute();
if(response.hasHeader("Location")){
System.out.println("Login ok!");
} else{
System.out.println("Login not ok!");
}
完整代码:
package com.github.davidepastore.stackoverflow34220319;
import java.io.IOException;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
/**
* Stackoverflow 34220319 question.
*
*/
public class App
{
public static void main( String[] args ) throws IOException
{
String urlLogin = "http://www.ime.org.ir/members/entrancecheck.aspx";
String userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36";
System.out.println("First request...");
Connection.Response response = Jsoup.connect(urlLogin)
.method(Connection.Method.GET)
.userAgent(userAgent)
.timeout(0)
.execute();
Document loginPage = response.parse();
System.out.println("Second request...");
response = Jsoup.connect(urlLogin)
.data("__LASTFOCUS", "")
.data("__EVENTTARGET", "")
.data("__EVENTARGUMENT","")
.data("__VIEWSTATE", loginPage.getElementById("__VIEWSTATE").val())
.data("__PREVIOUSPAGE", loginPage.getElementById("__PREVIOUSPAGE").val())
.data("__EVENTVALIDATION", loginPage.getElementById("__EVENTVALIDATION").val())
.data("ctl00$ContentPlaceHolder$entrancecheck1$username", "21465")
.data("ctl00$ContentPlaceHolder$entrancecheck1$pass", "amir1365")
.data("ctl00$ContentPlaceHolder$entrancecheck1$Button1", "ورود به پايگاه")
.userAgent(userAgent)
.followRedirects(false)
.cookies(response.cookies())
.method(Connection.Method.POST)
.timeout(0)
.execute();
if(response.hasHeader("Location")){
System.out.println("Login ok!");
} else{
System.out.println("Login not ok!");
}
Document doc = response.parse();
Elements description = doc
.select("table[id=table2]");
// Locate the content attribute
String desc = description.html();
}
}
感谢Stephan指出了错误的表单名称。