java爬虫:在请求body中增加json数据采集

时间:2024-01-09 13:46:38

1,http://www.hqepay.com/public/expressquery.html

查询快递不是将键值对post过去,而是将json数据放到body中发送过去。抓包如下:

java爬虫:在请求body中增加json数据采集

2,需要导入一些包,代码如下:

import java.io.UnsupportedEncodingException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils; public class Main { public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub DefaultHttpClient client = new DefaultHttpClient();
String url = "http://www.hqepay.com/common/WebAdapter.aspx";
HttpPost request = new HttpPost(url);
request.addHeader("Accept", "application/json, text/javascript, */*; q=0.01");
request.addHeader("X-Requested-With","XMLHttpRequest");
request.addHeader("Referer", "http://www.hqepay.com/public/expressquery.html?ECode=ZJS&barNo=8466878151&lab=0");
request.addHeader("Host","www.hqepay.com");
String param = "{\"FunClassName\":\"HqewPay.ExpBLL.ExpOnlineOrderBLL\",\"FunMethodName\":\"IndexTraceInfo\",\"ParamClassName\":\"HqewPay.Express.ExpParam\",\"ParamType\":\"Entity\",\"ParamData\":\"{\\\"ExpNo\\\":\\\"8466878151\\\",\\\"ExpCode\\\":\\\"\\\",\\\"ExpName\\\":\\\"ZJS\\\",\\\"parentCode\\\":\\\"ZJS\\\"}\"}";
StringEntity se = new StringEntity(param);
request.setEntity(se);
HttpResponse httpResponse = client.execute(request);
String retSrc = EntityUtils.toString(httpResponse.getEntity());
System.out.println(retSrc); } }