当前位置:   article > 正文

封装HTTP请求_.net 封装一个file类型的http请求帮助类

.net 封装一个file类型的http请求帮助类

 

  1. /**
  2. * post请求
  3. *
  4. * @param uri
  5. * @return
  6. * @throws IOException
  7. */
  8. public static HttpResponse doPost(String uri, String token) throws IOException {
  9. // 创建httpClient对象
  10. HttpClient httpclient = HttpClients.createDefault();
  11. // 设置请求地址
  12. HttpPost httppost = new HttpPost(uri);
  13. // 设置请求头
  14. httppost.addHeader("Content-Type", HttpConstants.HEADER_CONTENT_TYPE_JSON);
  15. httppost.addHeader("Authorization", token);
  16. // 执行http请求
  17. return httpclient.execute(httppost);
  18. }
  1. /**
  2. * post请求
  3. *
  4. * @param params 参数
  5. * @param uri uri
  6. * @return {@link HttpResponse}
  7. */
  8. public static HttpResponse doPostReq(String params, String uri) throws IOException {
  9. // 创建httpClient对象
  10. HttpClient httpclient = HttpClients.createDefault();
  11. // 设置请求地址
  12. HttpPost httppost = new HttpPost(uri);
  13. // 设置请求头
  14. httppost.addHeader("Content-Type", HttpConstants.HEADER_CONTENT_TYPE_JSON);
  15. // 设置请求内容
  16. StringEntity se = new StringEntity(params, "utf-8");
  17. httppost.setEntity(se);
  18. // 执行http请求
  19. return httpclient.execute(httppost);
  20. }
  1. public final class HttpConstants {
  2. public static final String HEADER_CONTENT_TYPE_JSON = "application/json;charset=UTF-8";
  3. public static final Integer SUCCESS_CODE = 200;
  4. public HttpConstants() {}
  5. }
  1. private boolean toGetInventoryInfo(Map<String, Object> reqMap) throws IOException {
  2. String params = JSON.toJSONStringWithDateFormat(reqMap,
  3. DateUtil.FORMAT_DATE_TIME
  4. , SerializerFeature.DisableCircularReferenceDetect);
  5. String inventoryUrl = aeonProperties.getInventory();
  6. log.info("请求,params:{},inventoryUrl:{}", params, inventoryUrl);
  7. HttpResponse response = HttpUtils.doPostReq(params, inventoryUrl);
  8. log.info("请求结果,response:{}", response.toString());
  9. if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  10. String result = EntityUtils.toString(response.getEntity(), "utf-8");
  11. log.info("请求数据结果,result:{}", result);
  12. List<Map> maps = JSONObject.parseArray(result, Map.class);
  13. if(!CollectionUtils.isEmpty(maps)){
  14. Map<String, Object> map = maps.get(0);
  15. return (Boolean) map.get("isRepertory");
  16. }
  17. }else {
  18. log.info("请求失败,params:{},inventoryUrl:{}", params, inventoryUrl);
  19. }
  20. return false;
  21. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/259352
推荐阅读
相关标签
  

闽ICP备14008679号