HttpClient request payload post請求
阿新 • • 發佈:2019-01-05
最近在做一些爬蟲相關的工作,遇到了一個網站,在使用谷歌開發者工具監控的時候,發現他的請求是這樣的
我們都知道傳統的post請求,所提交的是form data 格式的,key-value鍵值對的形式提交。這個在爬蟲post請求中非常常見。
第一次遇到了這種形式提交的,後面去找了很多方法,發現在百度搜的答案都用不了,後面翻牆去了stackoverflow,這邊貼上地址
http://stackoverflow.com/questions/22671897/httpclient-httppost-and-payload
所給的答案我也用不了。
於是去認真研究了一下,發現他所提交的post流,不是傳統的key-value模型的資料,而是post一段json文字
仔細查看了HttpClient的一些相關的API後,找到了答案
這邊貼上我的小Demo,Demo是用來抓取某個城市的公積金資料的,這邊隱藏去賬號密碼
- String str = "password="+"1234"+"\n"
- +"theName="+"XXX先生"+"\n"
- +"year=0\n"
- +"eTime="+DateUtil.getDate()+"\n"
- +"sfz="+"idCardxxxx"+"\n"
-
+"orderby=1\n"
- +"sTime=2000-01-01\n";
- String loginUrl = "http://www.lszfgjj.gov.cn/ajax/GuestWebGjj_searchUser,App_Web_10rvbinu.ashx?_method=getSearch&_session=r";
- HttpClient httpClient = new HttpClient();
- PostMethod post = new PostMethod(loginUrl);
-
post.setRequestHeader("Accept"
- post.setRequestHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36");
- post.setRequestHeader("Accept-Language","zh-cn,zh;q=0.8");
- post.setRequestHeader("Accept-Encoding","gzip, deflate, sdch");
- post.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
- post.setRequestHeader("Host","www.lszfgjj.gov.cn");
- post.setRequestHeader("Connection","keep-alive");
- post.setRequestHeader("Referer","http://www.lszfgjj.gov.cn/GuestWebGjj/default.aspx");
- RequestEntity entity = new StringRequestEntity(str, "text/html", "utf-8");
- post.setRequestEntity(entity);
- httpClient.executeMethod(post);
- String html = post.getResponseBodyAsString();
- System.out.println(html);
- RequestEntity entity = new StringRequestEntity(str, "text/html", "utf-8");
- post.setRequestEntity(entity);
以下是需要import的包:
- import org.apache.commons.httpclient.HttpClient;
- import org.apache.commons.httpclient.methods.PostMethod;
- import org.apache.commons.httpclient.methods.RequestEntity;
- import org.apache.commons.httpclient.methods.StringRequestEntity;
希望能幫到各位。點選開啟連結