使用 Httpclient 进行登录页面验证并导航到下一页 [英] Login page authentication with Httpclient and navigate to next page
问题描述
我想登录本网站 具有用户名和密码验证以及登录按钮的站点.此代码对于登录身份验证是否正确.此代码将帮助我导航到下一页?当我尝试获取导航页面的 url 时,它返回页面 url 中的登录信息.
I want to login to This website site which has username and password authentication and a login button. Is this code correct for login authentication. This code will help me navigate to next page? When I try to get the url of the navigated page it returns the login in page url.
String UrlLink ="http://postimage.org/profile.php";
protected Object doInBackground(Object... params) {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(UrlLink);
List<NameValuePair> login = new ArrayList<NameValuePair>();
login.add(new BasicNameValuePair("login", "xxxxxx"));
login.add(new BasicNameValuePair("password", "yyyyyyy"));
login.add(new BasicNameValuePair("Login", "Login"));
httpPost.setEntity(new UrlEncodedFormEntity(login));
HttpResponse response = httpClient.execute(httpPost);
int code = response.getStatusLine().getStatusCode();
Log.v("Document", httpPost.getURI().toString());
}
catch (Exception e) {
Log.v("Error", e.toString());
}
return null;
}
推荐答案
假设服务器端 PHP 代码在成功登录后发送 302
重定向标头,您可以将这些代码添加到您的 doInBackground()
触发客户端重定向的方法(如果您使用的是 HTTP Client 4.1+).
Assuming that the server-side PHP codes are sending a 302
redirect header upon successful login, you can add these codes to your doInBackground()
method to trigger a client-side redirect (if you are using HTTP Client 4.1+).
DefaultHttpClient client = new DefaultHttpClient();
client.setRedirectStrategy(new LaxRedirectStrategy());
这篇关于使用 Httpclient 进行登录页面验证并导航到下一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!