首先获取验证码并保存Cookie,登陆时将Cookie和账号密码一同发送出去,返回状态码200,登陆成功,接下来再去访问其他需要登录权限的页面时附上Cookie发送出去即可。
要实现模拟登陆,首先需要了解登陆网站时请求中都包含什么信息,需要用到的工具是[url=]HttpWatch[/url],这是抓包需要的工具,然后还有一个jar包,叫[url=]Jsoup[/url],这是用来解析网页HTML代码的。其次要用到抓取网页的类HttpClient。
要登录的网站为http://city.dlut.edu.cn/
登陆页面为http://cityjw.dlut.edu.cn:7001/ACTIONLOGON.APPPROCESS
通过[url=]HttpWatch[/url]可以看到
POST的数据为账号 密码 验证码 和两个无关的坐标数据
查看Cookies选项可以查看到Cookie
现在我们已经知道了我们POST的数据是什么,接下来便可以开工敲代码了。
首先获得验证码 [mw_shl_code=java,true]public Bitmap getcode() throws Exception {
HttpPost httpPost = new HttpPost("http://cityjw.dlut.edu.cn:7001/ACTIONVALIDATERANDOMPICTURE.APPPROCESS");
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;
}[/mw_shl_code]
然后将Cookie和账号密码一同加入到请求,发送给网站服务器
[mw_shl_code=java,true]public String login(String name, String psw, String code) {
HttpResponse httpResponse;
String uriAPI = "http://cityjw.dlut.edu.cn:7001/ACTIONLOGON.APPPROCESS";
/* 建立HTTP Post连线 */
HttpPost httpRequest = new HttpPost(uriAPI);
List<NameValuePair> params = new ArrayList<NameValuePair>();
String result = null;
params.add(new BasicNameValuePair("Agnomen", code));
params.add(new BasicNameValuePair("Password", psw));
params.add(new BasicNameValuePair("submit.x", "25"));
params.add(new BasicNameValuePair("submit.y", "6"));
params.add(new BasicNameValuePair("WebUserNO", name));
httpRequest.setHeader("Cookie", "JSESSIONID=" + COOKIE);
try {
// 发出HTTP request
httpRequest.setEntity(new UrlEncodedFormEntity(params, "GBK"));
// 取得HTTP response
httpResponse = client.execute(httpRequest); // 执行
// 若状态码为200 ok
if (httpResponse.getStatusLine().getStatusCode() == 200) { // 返回值正常
StringBuffer sb = new StringBuffer();
HttpEntity entity = httpResponse.getEntity();
String content = EntityUtils.toString(entity);
InputStream is = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "GBK"));
String data = "";
while ((data = br.readLine()) != null) {
sb.append(data);
}
result = sb.toString(); // 此时result中就是登陆后的页面的HTML的源代码了
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}[/mw_shl_code]
该方法返回的字符串为登陆后的页面源码
提取页面有用信息可以使用Jsoup,正则表达式也可以,不过相对麻烦一点
要实现模拟登陆,首先需要了解登陆网站时请求中都包含什么信息,需要用到的工具是[url=]HttpWatch[/url],这是抓包需要的工具,然后还有一个jar包,叫[url=]Jsoup[/url],这是用来解析网页HTML代码的。其次要用到抓取网页的类HttpClient。
要登录的网站为http://city.dlut.edu.cn/
登陆页面为http://cityjw.dlut.edu.cn:7001/ACTIONLOGON.APPPROCESS
通过[url=]HttpWatch[/url]可以看到
POST的数据为账号 密码 验证码 和两个无关的坐标数据
查看Cookies选项可以查看到Cookie
现在我们已经知道了我们POST的数据是什么,接下来便可以开工敲代码了。
首先获得验证码 [mw_shl_code=java,true]public Bitmap getcode() throws Exception {
HttpPost httpPost = new HttpPost("http://cityjw.dlut.edu.cn:7001/ACTIONVALIDATERANDOMPICTURE.APPPROCESS");
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;
}[/mw_shl_code]
然后将Cookie和账号密码一同加入到请求,发送给网站服务器
[mw_shl_code=java,true]public String login(String name, String psw, String code) {
HttpResponse httpResponse;
String uriAPI = "http://cityjw.dlut.edu.cn:7001/ACTIONLOGON.APPPROCESS";
/* 建立HTTP Post连线 */
HttpPost httpRequest = new HttpPost(uriAPI);
List<NameValuePair> params = new ArrayList<NameValuePair>();
String result = null;
params.add(new BasicNameValuePair("Agnomen", code));
params.add(new BasicNameValuePair("Password", psw));
params.add(new BasicNameValuePair("submit.x", "25"));
params.add(new BasicNameValuePair("submit.y", "6"));
params.add(new BasicNameValuePair("WebUserNO", name));
httpRequest.setHeader("Cookie", "JSESSIONID=" + COOKIE);
try {
// 发出HTTP request
httpRequest.setEntity(new UrlEncodedFormEntity(params, "GBK"));
// 取得HTTP response
httpResponse = client.execute(httpRequest); // 执行
// 若状态码为200 ok
if (httpResponse.getStatusLine().getStatusCode() == 200) { // 返回值正常
StringBuffer sb = new StringBuffer();
HttpEntity entity = httpResponse.getEntity();
String content = EntityUtils.toString(entity);
InputStream is = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "GBK"));
String data = "";
while ((data = br.readLine()) != null) {
sb.append(data);
}
result = sb.toString(); // 此时result中就是登陆后的页面的HTML的源代码了
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}[/mw_shl_code]
该方法返回的字符串为登陆后的页面源码
提取页面有用信息可以使用Jsoup,正则表达式也可以,不过相对麻烦一点