目的: 是模拟登陆学校类似图书馆一类的网站,把网页版移植到Android上面
相信很多人都做了这个,但是我也看了一些文章
问题:点击登陆按钮,我获得的仍然是登陆页面
Required Header,发现Header里面是有一个Cookie的选项,这个Cookie的内容可以通过加载验证码的同时获得,其实这个网站的登录时候的Cookie就是"CheckCode="+"验证码"
这个使可以通过代码获得的
/***************************
* 获得验证码
****************************/
private static HttpClient client = new DefaultHttpClient();
private static String COOKIE = "";
public Bitmap getcode(String imageUrl) throws Exception {
// 这里的imageUrl就是验证码的图片地址
HttpPost httpPost = new HttpPost(imageUrl);
HttpResponse httpResponse = client.execute(httpPost);
COOKIE = ((AbstractHttpClient) client).getCookieStore().getCookies().get(0).getValue();
byte[] bytes = EntityUtils.toByteArray(httpResponse.getEntity());
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return bitmap;
}
Params
这里是我写的发送登陆按钮点击的sendPost()模拟登录的代码
public String sendPost(String url, String username, String password,String inputcode) {
Log.d("TAG", "In sendPost");
HttpPost httpRequest = new HttpPost(url);
DefaultHttpClient httpclient = new DefaultHttpClient();
String strResult = null;
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("__VIEWSTATE", "/wEPDwULLTEwNDc4NjU1NDZkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBQZpbWdCdG6NS6KwmwZCH1Dgj4NV2VW+2H5jQg=="));
params.add(new BasicNameValuePair("__EVENTVALIDATION", "/wEWBQLO0dBIAq+u6rYIAoTOnYUHAsKAo/kOArDJ/bQC6qjden08ITUO/dKQ9HUpHrMqhVM="));
params.add(new BasicNameValuePair("returnUrl", "Web/Auths/Index.aspx"));
params.add(new BasicNameValuePair("bCheckCode", "1"));
// 账户
params.add(new BasicNameValuePair("UserName", username));
// 密码
params.add(new BasicNameValuePair("UserPwd", password));
// 验证码
params.add(new BasicNameValuePair("InputCode", inputcode));
// 图片的左边,貌似是不要紧的
params.add(new BasicNameValuePair("imgBtn.x", "91"));
params.add(new BasicNameValuePair("imgBtn.y", "37"));
httpRequest.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
httpRequest.addHeader("Content-Type", "pplication/x-www-form-urlencoded");
httpRequest.addHeader("Referer", "http://10.80.34.137/");
httpRequest.setHeader("Cookie", "CheckCode=" + COOKIE);
try {
// 添加请求参数到请求对象
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
// 获得响应对象
HttpResponse httpResponse = httpclient.execute(httpRequest);
// 判断是否请求成功
Log.d("TAG",""+httpResponse.getStatusLine().getStatusCode());
if (httpResponse.getStatusLine().getStatusCode() == 302) {
// 获得响应返回Json格式数据
strResult = EntityUtils.toString(httpResponse.getEntity());
Log.d("TAG",strResult);
return strResult;
} else {
strResult = "错误响应:" + httpResponse.getStatusLine().toString();
}
} catch (ClientProtocolException e) {
strResult = "错误响应:" + e.getMessage().toString();
e.printStackTrace();
return strResult;
} catch (IOException e) {
strResult = "错误响应:" + e.getMessage().toString();
e.printStackTrace();
return strResult;
} catch (Exception e) {
strResult = "错误响应:" + e.getMessage().toString();
e.printStackTrace();
return strResult;
}
return strResult;
}
非常想问这个是有问题出现在哪里,是因为参数写的补全还是语法出现了问题,因为自己已经着实找不到办法了,所以只好像大家提问了,谢谢大家,能提供一下解决的思路就最好了,再次谢谢大家.提问要是不好,大家可以说,我会补充的
Please pay attention to the problem of urldecode or form-data-encode. I also had such a problem. Later, I directly used url.open and then directly wrote to the byte stream.
Compare the message information you submitted using the code with the message you submitted manually. In addition, you need to observe whether the request login page obtains some values for use when submitting.