使用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屋!