1. 获取__VIEWSTATE与__EVENTVALIDATION
/**
* @return 获取__VIEWSTATE与__EVENTVALIDATION
*/
public static List<NameValuePair> getViewstateAndEventvalidation(){
// 创建远程url连接对象
HttpURLConnection connection = null;
InputStream is = null;
BufferedReader br = null;
String result = null; // 返回结果字符串
try {
URL url = new URL(loginUrl);
// 通过远程url连接对象打开一个连接,强转成httpURLConnection类
connection = (HttpURLConnection) url.openConnection();
// 设置连接方式:GET
connection.setRequestMethod("GET");
// 设置连接主机服务器的超时时间:15000毫秒
connection.setConnectTimeout(15000);
// 设置读取远程返回的数据时间:60000毫秒
connection.setReadTimeout(60000);
// 发送请求
connection.connect();
// 通过connection连接,请求成功后获取输入流
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
// 封装输入流is,并指定字符集
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
// 存放数据
StringBuffer sbf = new StringBuffer();
String temp = null;
while ((temp = br.readLine()) != null) {
sbf.append(temp);
}
result = sbf.toString();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放资源
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
connection.disconnect(); // 关闭远程连接
}
Document doc = Jsoup.parse(result);
Element viewStateElement = doc.getElementById("__VIEWSTATE");
Element viewStateElement1 = doc.getElementById("__EVENTVALIDATION");
String viewStateValue = viewStateElement.attr("value");
String viewStateValue1 = viewStateElement1.attr("value");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("tbxPassWord", password));
params.add(new BasicNameValuePair("tbxUserId", userName));
params.add(new BasicNameValuePair("__VIEWSTATE", viewStateValue));
params.add(new BasicNameValuePair("__EVENTVALIDATION", viewStateValue1));
params.add(new BasicNameValuePair("ExtCode", ""));
params.add(new BasicNameValuePair("button_Login", ""));
return params;
}
2.访问下一个链接
返回页面html->Document并且转为字符串
/**
* 返回页面html->Document并且转为字符串
*
* @param url
* @return
*/
public static String getResponseBody(String url) {
String responseBody = "";
try {
// 创建HttpClient对象
DefaultHttpClient httpClient = new DefaultHttpClient(new PoolingClientConnectionManager());
HttpPost httpPost = new HttpPost(loginUrl);
// 添加form-data参数
List<NameValuePair> params = getViewstateAndEventvalidation();
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
// 执行请求
HttpResponse response = httpClient.execute(httpPost);
CookieStore cookieStore = null;
DefaultHttpClient httpClient1 = new DefaultHttpClient(new PoolingClientConnectionManager());
//注意,返回的结果的状态码是302,非200
if (response.getStatusLine().getStatusCode() == 302) {
// 获取CookieStore
cookieStore = httpClient.getCookieStore();
httpClient1.setCookieStore(cookieStore);
}
HttpGet nextRequest = new HttpGet(url);
// 执行下一个连接的请求
HttpResponse nextResponse = httpClient1.execute(nextRequest);
// 获取响应内容
HttpEntity entity1 = nextResponse.getEntity();
if (entity1 != null) {
responseBody = EntityUtils.toString(entity1, StandardCharsets.UTF_8);
// 释放实体内容资源
EntityUtils.consume(entity1);
}
// 关闭HttpClient
httpClient.close();
httpClient1.close();
} catch (Exception e) {
e.printStackTrace();
}
return responseBody;
}