我想做一个学校图书馆的手机客户端,图书馆网页http://222.24.94.225/gdlisnet/ReaderLogin.aspx登陆时需选择登录方式,学号和密码.然后登陆成功跳转到http://222.24.94.225/gdlisnet/ReaderTable.aspx.我使用一下传递参数,
String url = "http://222.24.94.225/gdlisnet/ReaderLogin.aspx?";
HttpPost httpre = new HttpPost(url);
List<NameValuePair> params = new ArrayList<NameValuePair>();
BasicNameValuePair pair1 = new BasicNameValuePair("DropDownList1", "借书证号");
BasicNameValuePair pair2 = new BasicNameValuePair("TextBox1", "我的学号");
BasicNameValuePair pair3 = new BasicNameValuePair("TextBox2", "我的密码");
params.add(pair1);
params.add(pair2);
params.add(pair3);
try {
HttpEntity entity = new UrlEncodedFormEntity(params,"utf-8");
httpre.setEntity(entity);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(httpre);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity());
//Looper.prepare();
tv.setText(result);
//Looper.loop();
} else {
tv.setText("应答错误");
}
} catch (UnsupportedEncodingException e) {
// TODO: handle exception
e.printStackTrace();
}catch(ClientProtocolException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
但是得不到登陆后的页面源代码,为何?
可能是因为图书馆网站使用了防抓取技术。您可以尝试添加请求头信息,模拟浏览器请求,试图绕过防抓取措施。添加请求头信息的代码如下:
httpre.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36");
httpre.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");