如何解决使用jsoup,尝试通过自动登录检索页面,但登录页面不断返回
我正在尝试为我的帐户自动登录和检索 usps 通知递送页面。使用 Java 1.11 和 jsoup 1.13.1。我正在获取登录页面,解析来自表单元素的输入参数,然后执行 post 方法以发布到登录名。我没有返回包含跟踪号和其他信息的 Informed Delivery 页面,而是再次返回登录页面。谁能看出我做错了什么?
try {
//grab login form page first
Response loginPageResponse =
Jsoup.connect("https://reg.usps.com/portal/login")
.userAgent(httpconnection.DEFAULT_UA)
.timeout(10 * 1000)
.followRedirects(true)
.execute();
//System.out.println(loginPageResponse.parse().html().substring(1,5000));
Elements els = loginPageResponse.parse().select("form");
String token = new String();
String hexAttr = new String();
for (Element inputElement : els) {
//System.out.println(inputElement);
Elements inputs = inputElement.select("input");
for (Element input : inputs) {
System.out.println(input);
if (input.attr("name").equals("token")) {
token = input.attr("value");
//System.out.println("inside loop,token = "+ token);
}
if (input.attr("name").length() == 64) {
hexAttr = input.attr("name");
}
//System.out.println(input);
}
}
//get the cookies from the response,which we will post to the action URL
Map<String,String> mapLoginPageCookies = loginPageResponse.cookies();
//lets make data map containing all the parameters and its values found in the form
System.out.println("token: "+ token);
Map<String,String> mapParams = new HashMap<String,String>();
mapParams.put("username","xxxxx");
mapParams.put("password","yyyyy");
mapParams.put("struts.token.name","token");
mapParams.put("token",token);
mapParams.put(hexAttr,"");
System.out.println("mapParams" + mapParams);
//URL found in form's action attribute
String strActionURL = "https://reg.usps.com/portal/login/entreg/LoginPortalAction";
Response responsePostLogin = Jsoup.connect(strActionURL)
//referrer will be the login page's URL
.referrer("https://reg.usps.com/portal/login")
.userAgent(httpconnection.DEFAULT_UA)
.timeout(10 * 1000)
.data(mapParams)
.method(Method.POST)
.cookies(mapLoginPageCookies)
.followRedirects(true)
.execute();
System.out.println("HTTP Status Code: " + responsePostLogin.statusCode());
//parse the document from response
Document document = responsePostLogin.parse();
System.out.println(document.html().substring(1,50000));
获得 200 返回码。部分输出看起来像这样,其中包括显示来自 GET 方法检索到的第一个登录页面的表单输入
<input type="hidden" name="struts.token.name" value="token">
<input type="hidden" name="token" value="long alphanumeric code here">
<input type="hidden" name="64-bit hex code here" value="">
<input type="text" name="username" maxlength="50" value="" tabindex="3" id="username" class="form-control" autocorrect="off" autocomplete="off" aria-describedby="helpBlock" autocapitalize="off">
<input type="password" name="password" size="45" maxlength="50" value="" tabindex="4" id="password" class="form-control" title="Password" autocomplete="off">
返回的页面应该有一个 Title 元素,表示 Informed Delivery。当我查看帖子响应中显示的输出时,没有跟踪 ID,并且有相同的表单元素和相同的输入元素,但令牌和十六进制值与从登录页面的第一个 GET 返回的值不同。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。