[英]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");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.