Java代码之Http和Https请求

话不多说

http请求代码

  public String doGet(String url,Map<String,String> map){
    CloseableHttpClient httpClient =  null;
    CloseableHttpResponse response = null;
    String userNo = null;
    try {
      URIBuilder uriBuilder = new URIBuilder(url);
      map.entrySet().stream()
          .forEach(entrySet -> uriBuilder.setParameter(entrySet.getKey(), entrySet.getValue()));
      HttpGet httpget = new HttpGet(uriBuilder.build());
      //构造请求头部参数,如果没有头部参数可以不需要
      httpget.setHeader("token",map.get("token"));
     //创建http请求
      httpClient = HttpClients.createDefault();
      response = httpClient.execute(httpget);
      if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        HttpEntity entity = response.getEntity();
        String result = EntityUtils.toString(entity);
        JSONObject jsonObject = JSONObject.parseObject(result);
        //返回值是一个json,然后将json字符串进行转化获取想要的信息。
        String data = JSONObject.toJSONString(jsonObject.get("data"));


      }
    } catch (IOException e) {
      e.printStackTrace();
    } catch (URISyntaxException e) {
      e.printStackTrace();
    }finally {
      if (response != null) {
        try {
          response.close();
        } catch (IOException e) {
          log.error(e.getMessage(), e);
        }
      }
      if (httpClient != null) {
        try {
        //关闭请求
          httpClient.close();
        } catch (IOException e) {
          log.error(e.getMessage(), e);
        }
      }
    }

  }

Https请求代码

public String doGet(String url,Map<String,String> map){
    CloseableHttpClient httpClient =  null;
    CloseableHttpResponse response = null;
    String userNo = null;
    try {
      URIBuilder uriBuilder = new URIBuilder(url);
      map.entrySet().stream()
          .forEach(entrySet -> uriBuilder.setParameter(entrySet.getKey(), entrySet.getValue()));
      HttpGet httpget = new HttpGet(uriBuilder.build());
      httpget.setHeader("token",map.get("token"));
      //创建一个Htpps请求
      httpClient = SSLClient.wrapClient();
      response = httpClient.execute(httpget);
      if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        HttpEntity entity = response.getEntity();
        String result = EntityUtils.toString(entity);
        JSONObject jsonObject = JSONObject.parseObject(result);
        String data = JSONObject.toJSONString(jsonObject.get("data"));
        JSONObject jsonObject1 = JSONObject.parseObject(data);
         userNo = JSONObject.toJSONString(jsonObject1.get("userNo"));
        userNo = userNo.replace("\"","");
      }
    } catch (IOException e) {
      e.printStackTrace();
    } catch (URISyntaxException e) {
      e.printStackTrace();
    }finally {
      if (response != null) {
        try {
          response.close();
        } catch (IOException e) {
          log.error(e.getMessage(), e);
        }
      }
      if (httpClient != null) {
        try {
          httpClient.close();
        } catch (IOException e) {
          log.error(e.getMessage(), e);
        }
      }
    }

    return userNo ;
  }

绕过证书 创建SSLClient

class SSLClient {
  /**
   * 绕过证书
   * @return 返回构造完成的client
   */
  public static CloseableHttpClient wrapClient() {
    try {
      SSLContext ctx = SSLContext.getInstance("TLS");
      X509TrustManager tm = new X509TrustManager() {
        @Override
        public X509Certificate[] getAcceptedIssuers() {
          return null;
        }

        @Override
        public void checkClientTrusted(X509Certificate[] arg0,
            String arg1) throws CertificateException {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] arg0,
            String arg1) throws CertificateException {
        }
      };
      ctx.init(null, new TrustManager[] { tm }, null);
      SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(
          ctx, NoopHostnameVerifier.INSTANCE);
      CloseableHttpClient httpclient = HttpClients.custom()
          .setSSLSocketFactory(ssf).build();
      return httpclient;
    } catch (Exception e) {
      return HttpClients.createDefault();
    }
  }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值