zoukankan      html  css  js  c++  java
  • java爬虫:在请求body中增加json数据采集

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

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

    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);
            
        }
    
    }
  • 相关阅读:
    日期计算
    普通二叉树转换成搜索二叉树
    每周行情
    virtualbox安装增强功能时【未能加载虚拟光盘】
    linux实用命令之如何移动文件夹及文件下所有文件
    Linux文件夹文件创建、删除
    php 克隆 clone
    function_exists (),method_exists()与is_callable()的区别
    webgrind安装使用详细说明
    windows下redis的安装配置和php扩展使用phpredis
  • 原文地址:https://www.cnblogs.com/wang7/p/4991190.html
Copyright © 2011-2022 走看看