java 实现通过 post 方式提交json参数操作

所属分类: 软件编程 / java 阅读数: 138
收藏 0 赞 0 分享

由于所爬取的网站需要验证码,通过网页的开发人员工具【F12】及在线http post,get接口测试请求工具(http://coolaf.com/)发现访问时加上请求头header 信息时可以跳过验证码校验。

而且该网站只接受post请求,对提交的参数也只接受json格式,否则请求失败。

现将通过 post 方式提交json参数的方法记录如下:

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * <p>@PostJsonParamsTest.java</p> 
 * @version 1.0
 * @author zxk
 * @Date 2018-3-3
 */
public class PostJsonParamsTest {

  // 超时时间
  private static final int RUN_TIME =10000;

  // 爬取初始页数
  private String page;

  public static void main(String[] args) throws Exception {
    PostJsonParamsTest crawl = new PostJsonParamsTest();

    // 请求的url地址
    String url ="http://www.gzcredit.gov.cn/Service/CreditService.asmx/searchOrgWithPage";
    // 设置起始访问页码
    crawl.setPage("1");
    String isStop = "";

    // 设置请求
    HttpRequestBase request = null;
    request = new HttpPost(url);

    try {
      // 设置config
      RequestConfig requestConfig = RequestConfig.custom()
            .setSocketTimeout(RUN_TIME)
            .setConnectTimeout(RUN_TIME)
            .setConnectionRequestTimeout(RUN_TIME)
            .build();
      request.setConfig(requestConfig);

      // json 格式的 post 参数
      String postParams ="{\"condition\":{\"qymc\":\"%%%%\",\"cydw\":\"\"},\"pageNo\":"+crawl.getPage()+",\"pageSize\":100,count:2709846}";
      System.out.println(postParams);
      HttpEntity httpEntity = new StringEntity(postParams);
      ((HttpPost) request).setEntity(httpEntity);

      // 添加请求头,可以绕过验证码
      request.addHeader("Accept","application/json, text/javascript, */*");
      request.addHeader("Accept-Encoding","gzip, deflate");
      request.addHeader("Accept-Language", "zh-CN,zh;q=0.8");
      request.addHeader("Connection", "keep-alive");
      request.addHeader("Host", "www.gzcredit.gov.cn");
      request.addHeader("Content-Type", "application/json; charset=UTF-8");

      URIBuilder builder = new URIBuilder(url);       
      URI uri = builder.build();
      uri = new URI(URLDecoder.decode(uri.toString(), "UTF-8"));
      request.setURI(uri);

      while(!isStop.equals("停止")||isStop.equals("重跑")){
        isStop = crawl.crawlList(request);
        if(isStop.equals("爬取")){
          crawl.setPage(String.valueOf(Integer.parseInt(crawl.getPage())+1));
        }

        // if("2713".equals(crawl.getPage())) break;
        if("2".equals(crawl.getPage())){
          break;
        }
      }
    } catch (NumberFormatException e) {
      e.printStackTrace();
      throw new NumberFormatException("数字格式错误");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
      throw new UnsupportedEncodingException("不支持的编码集");
    }
  }
  /**
   * 爬取搜索列表
   * @param page
   * @return
   */
  private String crawlList(HttpRequestBase request){
    int statusCode = 0;

    // 下面两种方式都可以用来创建客户端连接,相当于打开了一个浏览器
    CloseableHttpClient httpClient = HttpClients.createDefault(); 
    // HttpClient httpClient = HttpClientBuilder.create().build();

    HttpEntity httpEntity = null;
    HttpResponse response = null;
    try {      
      try {        
        response = httpClient.execute(request);
      } catch (Exception e){
        e.printStackTrace();
        EntityUtils.consumeQuietly(httpEntity);
        return "重跑";
      } 

      //打印状态
      statusCode =response.getStatusLine().getStatusCode();
      if(statusCode!=200){
        EntityUtils.consumeQuietly(httpEntity);
        return "重跑";
      }
      //实体
      httpEntity = response.getEntity();
      String searchListStr = EntityUtils.toString(httpEntity,"GBK").replaceAll("\\\\米", "米");
      String allData = (String) JSONObject.parseObject(searchListStr).get("d");
      // 字符串值中间含双引号的替换处理
      String s = allData.replaceAll("\\{\"","{'")
          .replaceAll("\":\"", "':'")
          .replaceAll("\",\"", "','")
          .replaceAll("\":", "':")
          .replaceAll(",\"", ",'")
          .replaceAll("\"\\}", "'}")
          .replaceAll("\"", "")
          .replaceAll("'", "\"")
          .replaceAll("<br />", "")        
          .replaceAll("\t", "")
          .replaceAll("\\\\", "?");
      JSONObject jsonData = JSONObject.parseObject(s);
      JSONArray jsonContent = jsonData.getJSONArray("orgList");

      searchListStr = null;
      allData = null; 
      s = null;

      if (jsonContent==null || jsonContent.size()<1) {
        return "重跑";
      }
      System.out.println(jsonContent.toJSONString());
      return "爬取";
    } catch (Exception e) {
      e.printStackTrace();
      return "重跑";
    } finally{
      EntityUtils.consumeQuietly(httpEntity);
    }
  }

  private String getPage() {
    return page;
  }

  private void setPage(String page) {
    this.page = page;
  }

}

补充知识:JAVA利用HttpClient发送post请求,将请求数据放到body里

我就废话不多说了,大家还是直接看代码吧~

  /**
   * post请求 ,请求数据放到body里
   * @param url  请求地址
   * @param bodyData 参数
   * @author wangyj
   * @date 2019年4月20日
   */
  public static String doPostBodyData(String url, String bodyData) throws Exception{
    String result = "";
    CloseableHttpClient httpClient = null;
    CloseableHttpResponse response = null;
    try {
      HttpPost httpPost = getHttpPost(url, null); // 请求地址
      httpPost.setEntity(new StringEntity(bodyData, Encoding));
      httpClient = getHttpClient();
      // 得到返回的response
      response = httpClient.execute(httpPost);
      HttpEntity entity = response.getEntity();
      result = getResult(entity, Encoding);
    } catch (Exception e) {
      throw e;
    } finally {
      // 关闭httpClient
      if (null != httpClient) {
        httpClient.close();
      }
      // 关闭response
      if (null != response) {
        EntityUtils.consume(response.getEntity()); // 会自动释放连接
        response.close();
      }
    }
    return result;
  }

以上这篇java 实现通过 post 方式提交json参数操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

详解Spring依赖注入:@Autowired,@Resource和@Inject区别与实现原理

这篇文章主要介绍了详解Spring依赖注入:@Autowired,@Resource和@Inject区别与实现原理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

了解spring中的CloudNetflix Hystrix弹性客户端

这篇文章主要介绍了了解spring中的CloudNetflix Hystrix弹性客户端,客户端弹性模式是在远程服务发生错误或表现不佳时保护远程资源(另一个微服务调用或者数据库查询)免于崩溃。,需要的朋友可以参考下
收藏 0 赞 0 分享

Spark学习笔记Spark Streaming的使用

这篇文章主要介绍了Spark学习笔记Spark Streaming的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

通过实例讲解springboot整合WebSocket

这篇文章主要介绍了通过实例讲解springboot整合WebSocket,WebSocket为游览器和服务器提供了双工异步通信的功能,即游览器可以向服务器发送消息,服务器也可以向游览器发送消息。,需要的朋友可以参考下
收藏 0 赞 0 分享

java虚拟机学习笔记进阶篇

在本篇内容里小编给大家分享了关于java虚拟机学习笔记的进阶内容,需要的朋友们跟着学习下。
收藏 0 赞 0 分享

java虚拟机学习高级篇

在本篇文章里小编给大家整理了关于java虚拟机学习高级篇的相关内容,有兴趣的朋友们跟着学习参考下。
收藏 0 赞 0 分享

java虚拟机中多线程总结

在本篇内容中小编给大家分享的是关于java虚拟机中多线程的知识点总结内容,需要的朋友们参考学习下。
收藏 0 赞 0 分享

java虚拟机多线程进阶篇总结

在本篇内容里小编给大家整理了关于java虚拟机多线程进阶篇的相关知识点内容,有兴趣的朋友们跟着参考下。
收藏 0 赞 0 分享

java数据结构和算法中数组的简单入门

在本文里小编给大家整理了关于java数据结构和算法中数组的简单入门知识点整理,需要的朋友们学习下。
收藏 0 赞 0 分享

java数据结构和算法中哈希表知识点详解

在本篇文章里小编给大家分享了关于java数据结构和算法中哈希表的相关知识点内容,需要的朋友们学习下。
收藏 0 赞 0 分享
查看更多