简体   繁体   English

使用Jsoup登录网站时遇到麻烦,

[英]Having trouble loggin into website with Jsoup,

I'm trying to log into a website with jsoup, I'm pretty sure I am parsing all the things I need to, I just can't figure out what's wrong. 我正在尝试使用jsoup登录一个网站,我很确定自己正在解析所有我需要的内容,但我只是想不出什么问题。

I am using this for reference: http://cs.harding.edu/fmccown/android/Logging-into-Pipeline.pdf 我正在使用它作为参考: http : //cs.harding.edu/fmccown/android/Logging-into-Pipeline.pdf

Here is the code that is in my AsycnTask doInBackground: 这是我的AsycnTask doInBackground中的代码:

            Connection.Response loginForm = Jsoup.connect(url)
                    .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                    .referrer("http://www.google.com")
                    .method(Connection.Method.GET)
                    .timeout(10000)
                    .execute();

            Document doc = loginForm.parse();

            //Random values you need to parse to lectio
            String VIEWSTATEXvalue = doc.select("input[name=__VIEWSTATEX").attr("value");
            String EVENTVALIDATIONvalue = doc.select("input[name=__EVENTVALIDATION").attr("value");

            Log.v("MainActivity",VIEWSTATEXvalue + EVENTVALIDATIONvalue);



            doc = Jsoup.connect(url)
                    .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                    .referrer("http://www.google.com")
                    .data("m$Content$username2", username)
                    .data("m$Content$password2", password)
                    .data("__VIEWSTATEX",VIEWSTATEXvalue)
                    .data("__EVENTVALIDATION",EVENTVALIDATIONvalue)
                    .data("__EVENTTARGET","m$Content$submitbtn2")

                    .cookies(loginForm.cookies())
                    .post();
            Log.v("MainActivity", doc.toString());

But the problem is it doesn't contain the document of the page when I am logged in, it contains the document for an error page that just says "something went wrong". 但是问题在于,当我登录时它不包含页面的文档,而是包含错误页面的文档,该页面仅显示“出了点问题”。

I'm guessing this is because I am not parsing all of the parameters. 我猜这是因为我没有解析所有参数。

This is the page I am trying to login to: https://www.lectio.dk/lectio/11/login.aspx?lecafdeling=4733693054 这是我要登录的页面: https : //www.lectio.dk/lectio/11/login.aspx?lecafdeling=4733693054

having looked into to some of forms being submitted this was something i could find: 调查了一些提交的表格后,我发现:

//<![CDATA[
var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
    theForm.__EVENTTARGET.value = eventTarget;
    theForm.__EVENTARGUMENT.value = eventArgument;
    theForm.submit();
}
}
//]]>

But I don't how to parse the proper value for these 但是我不怎么解析这些的正确价值

The easier way to see what is being sent by the browser is to inspect the raw http request and just copy the headers. 查看浏览器正在发送的内容的更简单方法是检查原始http请求,然后仅复制标头。 You can do this with Chrome by opening the Development tools by pressing F12 . 您可以通过按F12打开“开发工具”来使用Chrome来执行此操作。 Select Network . 选择Network There is a bullet on the upper left. 左上方有一个项目符号。 Hover it and it will display Record Network Log . 悬停它,它将显示Record Network Log This must be red. 必须是红色的。 If it's not press it. 如果不是,请按。 This will record all the traffic of the request you will send. 这将记录您将发送的请求的所有流量。 You will a number of rows on the table below. 您将在下表中找到许多行。 Sort by Method and select the row with value POST (Press on the value of the column Name . This is the actual request you are sending. On the panel on the right select Headers . Check the Request Headers and Form Data . Check if all of these values are the same. This will help you in order to hardcode some values that you are missing. 按“ Method排序,然后选择值为POST的行(按“ Name ”列的值。这是您要发送的实际请求。在右侧面板上选择“ Headers 。检查“ Request HeadersForm Data 。检查是否全部这些值是相同的,这将有助于您对一些缺失的值进行硬编码。

我不得不用

String EVENTVARGUMENTvalue = doc.select("input[name=__EVENTARGUMENT").attr("value");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM