繁体   English   中英

如何在Android中使用jsoup自动登录网页?

[英]How to Automate login to a webpage using jsoup in android?

我试图使用jsoup为我的用户自动登录http://www.bvrit.edu.in,并使用webview为我的用户显示已登录的网页。我添加了jsoup API,并使用inspect元素进行了检查usename字段的ID为txtId1,密码为txtPwd1,并用相应的名称替换帖子中的数据。我还添加了互联网访问权限来显示,但我无法显示网页,我的代码如下所示,我认为我弄错了一些基础知识,但无法弄清楚。

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState)  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void main(String[] args) throws IOException {

    WebView browser = (WebView) findViewById(R.id.bvritWebview);

    Connection.Response loginForm = Jsoup.connect("http://www.bvrit.edu.in/")
            .ignoreContentType(true)
            .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
            .referrer("http://www.google.com")
            .timeout(12000)
            .followRedirects(true)
            .method(Connection.Method.GET)
            .execute();

    Connection.Response loginFormFilled = Jsoup.connect("http://www.bvrit.edu.in/")
            .ignoreContentType(true)
            .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
            .followRedirects(true)
            .referrer("https://login.to/")
            .data("txtId1", "username")//check the form to find field name for user name
            .data("txtPwd1", "password")//check the form to find field name for user password
            .cookies(loginForm.cookies())
            .method(Connection.Method.POST)
            .execute();
    int statusCode = loginFormFilled.statusCode();
    Map<String, String> cookies = loginFormFilled.cookies();
    browser.getSettings().setJavaScriptEnabled(true);
    browser.loadUrl("http://www.bvrit.edu.in");

}

}

登录后的网络标题部分- 在此处输入图片说明

您在POST请求中缺少一些参数。 在浏览器中加载页面,按F12键启动开发人员工具,然后查看POST请求。 您会看到类似这样的内容-
POST请求

您必须将所有这些参数发送到服务器,而不仅仅是现在发送的参数。
前三个参数对于每个会话都是唯一的,您可以从第一个GET请求中获取它们,类似这样(CSS选择器可能有所不同,我没有在您的URL上尝试过):

Document doc = loginForm.parse();
Element e = doc.select("input[id=__VIEWSTATE]").first();
String viewState = e.attr("value");

暂无
暂无

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

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