Jsoup模拟发起restful请求

  • Jsoup版本:compile(‘org.jsoup:jsoup:1.11.2’)
  • httpclient 版本:compile(“org.apache.httpcomponents:httpclient:4.5.1”)
  • httpcore 版本:compile(“org.apache.httpcomponents:httpcore:4.4.3”)
  • 文件上传须添加 httpmime包: compile(“org.apache.httpcomponents:httpmime:4.5.1”)

1、模拟post请求:

/**
 * POST json
 */

public void postJsonTest(String url) {
  try {
    Connection connect = Jsoup.connect(url);//请求链接
    //以requestBody的形式发送请求 非表单形式提交,表单形式须修改"Content-Type"
    VO vo= new VO();
    System.out.println("请求报文:" + JSON.toJSONString(vo));
    Connection requestBody = connect.requestBody(JSON.toJSONString(vo));
    requestBody.ignoreHttpErrors(true);
    requestBody.validateTLSCertificates(false);
    requestBody.method(Connection.Method.POST);
    requestBody.ignoreContentType(true);
    requestBody.postDataCharset("utf-8");
    // Content-Type=application/json
    requestBody.header("Content-Type", "application/json");
    Connection.Response execute = requestBody.execute();
    String body1 = execute.body();
    System.out.println("返回报文:" + body1);
    JSONObject js = JSON.parseObject(body1);
  } catch (Exception ex) {
    ex.printStackTrace();
  }
}

2、模拟文件上传:

@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
public ResponseEntity fileUpload2(@RequestParam("file") MultipartFile file) {
  logger.info("OprUploadFileRestApiController -> fileUpload");
  String localDir = "${OPR_UPLOAD_DIR}";//文件上传的目录设置
  if (StringUtils.isBlank(localDir)) {
    return new ResponseEntity<>(new ResponseEnvelope<>("请检查文件上传的目录设置"), HttpStatus.OK);
  }
  String fileName = null;
  String filePath = null;
  String newFileName = UUID.randomUUID().toString().trim().replaceAll("-", "");

  // 判断文件是否为空
  if (!file.isEmpty()) {
    try {
      fileName = file.getOriginalFilename();
      filePath = localDir + "/" + newFileName;
      if (fileName.indexOf(".") < 0) {
        return new ResponseEntity<>(new ResponseEnvelope<>("文件类型错误"), HttpStatus.OK);
      }
      // 文件保存路径
      // 转存文件
      logger.info("文件名:{} 文件路径:{} 保存到硬盘",fileName,filePath);
      file.transferTo(new File(filePath));
      return new ResponseEntity<>(new ResponseEnvelope<>(newFileName), HttpStatus.OK);
    } catch (Exception e) {
      return new ResponseEntity<>(new ResponseEnvelope<>("文件保存失败"), HttpStatus.OK);
    }
  } else {
    return new ResponseEntity<>(new ResponseEnvelope<>("上传文件为null"), HttpStatus.OK);
  }
}
/**
 * POST MultipartFile文件上传
 */

public void uploadFileByPost(String url, String fileLocation) throws Exception {
  CloseableHttpClient httpclient = HttpClients.createDefault();
  HttpPost httppost = new HttpPost(url);
  File file = new File(fileLocation);//WebFileUtils.createFileByUrl(url, "jpg");
  System.out.println("executing request " + httppost.getURI());
  //setConnectTimeout:设置连接超时时间,单位毫秒。
  // setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间,单位毫秒。
  // setSocketTimeout:请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
  RequestConfig defaultRequestConfig = RequestConfig.custom()
      .setConnectTimeout(5000)
      .setConnectionRequestTimeout(5000)
      .setSocketTimeout(15000)
      .build();
  httppost.setConfig(defaultRequestConfig);
  Map<String,ContentBody> reqParam = new HashMap<String,ContentBody>();
  reqParam.put("filename", new StringBody("Hydrangeas.jpg", ContentType.MULTIPART_FORM_DATA));
  reqParam.put("file", new FileBody(file));
  MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
  multipartEntityBuilder.setCharset(Charset.forName("utf-8"));
  multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
  for(Map.Entry<String,ContentBody> param : reqParam.entrySet()){
    multipartEntityBuilder.addPart(param.getKey(), param.getValue());
  }
  HttpEntity reqEntity = multipartEntityBuilder.build();
  httppost.setEntity(reqEntity);
  // 执行post请求.
  CloseableHttpResponse response = httpclient.execute(httppost);
  System.out.println("got response");
  try {
    // 获取响应实体
    HttpEntity entity = response.getEntity();
    //System.out.println("--------------------------------------");
    // 打印响应状态
    //System.out.println(response.getStatusLine());
    if (entity != null) {
      System.out.println(EntityUtils.toString(entity, Charset.forName("UTF-8")));
    }
    //System.out.println("------------------------------------");
  } finally {
    response.close();
  }
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注