最近在弄人才引进入户广州事宜,资料已经提交上去了,但不知啥时候才到公示,也不知自己在第几页,所以闲来无事写了一个查询脚本,一运行很快就知道自己有没有在公示了。
人才引进名单公示:
http://www.hrssgz.gov.cn/vsgzpiapp01/GZPI/Gateway/PersonIntroducePublicity.aspx
第一步右键查看网页源码:
第二步:使用fiddler抓包工具进行抓包分析
发现如下参数:
第三步:编写Java脚本
代码如下:
package com.pk4pk.inline;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/**
*
* @Author pk4pk
* @Email 401885064@qq.com
* @Date 2017年9月13日
* @TODO 人才引进查找是否有我
*/
public class FindMe {
private final static String url="http://www.hrssgz.gov.cn/vsgzpiapp01/GZPI/Gateway/PersonIntroducePublicity.aspx";
private static String view_state = "";
public static void main(String[] args) throws IOException, InterruptedException {
// TODO Auto-generated method stub
int pageIndex = 0;
while (true) {
System.out.println("没有找到。。。。");
if (getHttpString(pageIndex).contains("<td>这里输入你的名字</td>")) {
break;
}
pageIndex++;
Thread.sleep(1000 * 3);
}
System.out.println("已经找到。。。。");
}
/**
* Post请求获取内容
* @param page
* @return
* @throws IOException
*/
private static String getHttpString(int page) throws IOException {
System.out.println("page :" + page);
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpPost httpPost = new HttpPost(url);
if(page!=0){
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("__EVENTTARGET", "NextLBtn"));
nvps.add(new BasicNameValuePair("__EVENTARGUMENT", ""));
nvps.add(new BasicNameValuePair("__VIEWSTATE", view_state));
nvps.add(new BasicNameValuePair("__VIEWSTATEGENERATOR", "3C84D956"));
nvps.add(new BasicNameValuePair("ToPage", "" + page));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
System.out.println("Executing request " + httpPost.getRequestLine());
}
// Create a custom response handler
ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
@Override
public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
}
};
String responseBody = httpclient.execute(httpPost, responseHandler);
String resultResponseBody =responseBody;
System.out.println("-----------------page :"+page+"-----------------------");
System.out.println(responseBody);
//截取__VIEWSTATE的值
int index = responseBody.indexOf("<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"");
System.out.println("index :" + index);
responseBody=responseBody.substring(index+64, responseBody.length());
int endIndex=responseBody.indexOf("\"");
System.out.println("endIndex :" + endIndex);
view_state=responseBody.substring(0,endIndex);
System.out.println("view_state :"+view_state);
return resultResponseBody;
} finally {
httpclient.close();
}
}
}
总结:关键要耐心和仔细分析抓包的数据,就像上面的__VIEWSTATE值,一开始以为只要改下ToPage字段就可以了,发现请求下来的都是同一页数据,后面再看网页源码才发现原来还需要_VIEWSTATE 值