Jsoup 得到不同的 HTML,只有默认查询页面,而不是结果页面
Jsoup get different HTML,only default query page, not result page
我正在尝试从网站检索一些停车数据以显示在 Android 中。
现在我正在尝试从此站点查询一些数据 (AB-5678)
E-BILL(中文页面)。
查询页和结果页都使用相同的URL.
Chrome 控制台
以下是我post在Chrome控制台查询成功后的内容。
(Header)
(数据)
我尝试使用 Chrome 插件 - POSTMAN 再次查询,他也给了我一个成功的结果页面。
但是,当我利用 Jsoup 查询数据时,如下所示
Connection.Response resource = Jsoup.connect("https://ebill.ba.org.tw/CPP//DesktopDefault.aspx?TabIndex=4&TabId=153")
.timeout(Define.WEBTIMEOUT)
.method(Connection.Method.GET)
.execute();
Document document = Jsoup.connect("https://ebill.ba.org.tw/CPP//DesktopDefault.aspx?TabIndex=4&TabId=153")
.data("__EVENTTARGET", "")
.data("__EVENTARGUMENT", "")
.data("__VIEWSTATE", resource.parse().getElementById("__VIEWSTATE").val())
.data("__VIEWSTATEGENERATOR", resource.parse().getElementById("__VIEWSTATEGENERATOR").val())
.data("ctl00$Desktopthreepanes1$ThreePanes$ctl13$VehicleType", "C")
.data("ctl00$Desktopthreepanes1$ThreePanes$ctl13$CarNo", "AB-5678")
.data("ctl00$Desktopthreepanes1$ThreePanes$ctl13$OkButton", "")
.referrer("https://ebill.ba.org.tw/CPP//DesktopDefault.aspx?TabIndex=4&TabId=153")
.header("Cache-Control","no-cache")
.header("Host", "ebill.ba.org.tw")
.header("Origin", "https://ebill.ba.org.tw")
.header("Upgrade-Insecure-Requests", "1")
.header("Content-Type", "application/x-www-form-urlencoded")
.cookie("IsPassClientEnvironDetection", "true")
.cookie("IsPassMobileClientEnvironDetection","true")
.cookie("c","1450682673891")
.followRedirects(true)
.userAgent("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36")
.cookies(resource.cookies())
.timeout(Define.WEBTIMEOUT)
.post();
但是我得到的只是默认查询页面。
我用谷歌搜索了所有可能的答案,我不认为这个结果来自 JavaScript 的 cookies,而且我已经设置了 userAgent.....我错过了什么吗?
我知道哪里出了问题。
我试图分析每个 post 的 content-length,在 Chrome 中的值是 ~1100,但在我的 jsoup 中只有 ~500。
跟踪后,我发现 _VIEWSTATE val 长度应该是 700+ 但在 jsoup 中总是 200...
最后,我发现cookies应该设置在[GET]阶段,否则这个网站会给你一个错误的__VIEWSTATE值。
即
Connection.Response resource = Jsoup.connect("https://ebill.ba.org.tw/CPP//DesktopDefault.aspx?TabIndex=4&TabId=153")
.timeout(Define.WEBTIMEOUT)
.method(Connection.Method.GET)
.execute();
应该是...
Connection.Response resource = Jsoup.connect("https://ebill.ba.org.tw/CPP//DesktopDefault.aspx?TabIndex=4&TabId=153")
.cookie("IsPassClientEnvironDetection", "true")
.cookie("IsPassMobileClientEnvironDetection", "true")
.timeout(Define.WEBTIMEOUT)
.method(Connection.Method.GET)
.execute();
我正在尝试从网站检索一些停车数据以显示在 Android 中。
现在我正在尝试从此站点查询一些数据 (AB-5678)
E-BILL(中文页面)。
查询页和结果页都使用相同的URL.
Chrome 控制台
以下是我post在Chrome控制台查询成功后的内容。
(Header)
(数据)
我尝试使用 Chrome 插件 - POSTMAN 再次查询,他也给了我一个成功的结果页面。
但是,当我利用 Jsoup 查询数据时,如下所示
Connection.Response resource = Jsoup.connect("https://ebill.ba.org.tw/CPP//DesktopDefault.aspx?TabIndex=4&TabId=153")
.timeout(Define.WEBTIMEOUT)
.method(Connection.Method.GET)
.execute();
Document document = Jsoup.connect("https://ebill.ba.org.tw/CPP//DesktopDefault.aspx?TabIndex=4&TabId=153")
.data("__EVENTTARGET", "")
.data("__EVENTARGUMENT", "")
.data("__VIEWSTATE", resource.parse().getElementById("__VIEWSTATE").val())
.data("__VIEWSTATEGENERATOR", resource.parse().getElementById("__VIEWSTATEGENERATOR").val())
.data("ctl00$Desktopthreepanes1$ThreePanes$ctl13$VehicleType", "C")
.data("ctl00$Desktopthreepanes1$ThreePanes$ctl13$CarNo", "AB-5678")
.data("ctl00$Desktopthreepanes1$ThreePanes$ctl13$OkButton", "")
.referrer("https://ebill.ba.org.tw/CPP//DesktopDefault.aspx?TabIndex=4&TabId=153")
.header("Cache-Control","no-cache")
.header("Host", "ebill.ba.org.tw")
.header("Origin", "https://ebill.ba.org.tw")
.header("Upgrade-Insecure-Requests", "1")
.header("Content-Type", "application/x-www-form-urlencoded")
.cookie("IsPassClientEnvironDetection", "true")
.cookie("IsPassMobileClientEnvironDetection","true")
.cookie("c","1450682673891")
.followRedirects(true)
.userAgent("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36")
.cookies(resource.cookies())
.timeout(Define.WEBTIMEOUT)
.post();
但是我得到的只是默认查询页面。
我用谷歌搜索了所有可能的答案,我不认为这个结果来自 JavaScript 的 cookies,而且我已经设置了 userAgent.....我错过了什么吗?
我知道哪里出了问题。
我试图分析每个 post 的 content-length,在 Chrome 中的值是 ~1100,但在我的 jsoup 中只有 ~500。
跟踪后,我发现 _VIEWSTATE val 长度应该是 700+ 但在 jsoup 中总是 200...
最后,我发现cookies应该设置在[GET]阶段,否则这个网站会给你一个错误的__VIEWSTATE值。
即
Connection.Response resource = Jsoup.connect("https://ebill.ba.org.tw/CPP//DesktopDefault.aspx?TabIndex=4&TabId=153")
.timeout(Define.WEBTIMEOUT)
.method(Connection.Method.GET)
.execute();
应该是...
Connection.Response resource = Jsoup.connect("https://ebill.ba.org.tw/CPP//DesktopDefault.aspx?TabIndex=4&TabId=153")
.cookie("IsPassClientEnvironDetection", "true")
.cookie("IsPassMobileClientEnvironDetection", "true")
.timeout(Define.WEBTIMEOUT)
.method(Connection.Method.GET)
.execute();