欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Java发送https请求代码实例

时间:2022-03-27 08:30:28|栏目:JAVA代码|点击:

1、前文:通过webService发送https请求,有两种版本,一种是携带证书验证(比较麻烦),另外一种就是直接忽略证书,本文提供的就是第二种(本人已测试过)

2、最简易代码:

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;

@SuppressWarnings("all")
public class TestAPI_https {
  public static void main(String args[]) throws Exception {
    new TestAPI_https().TestRiQingAPI_SaleOrder();
  }

  public static void TestRiQingAPI_SaleOrder() throws Exception {
    
    String postData = getJson();
    //String url = "https://*****";
    String url = "https://*****";
    HttpsURLConnection conn = null;
    OutputStream out = null;
    String rsp = null;
    byte[] byteArray = postData.getBytes("utf-8");
    try {
      URL uri = new URL(url);
      conn = (HttpsURLConnection) uri.openConnection();
      //忽略证书验证--Begin
      conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
      //忽略证书验证--End
      conn.setRequestMethod("POST");
      conn.setDoInput(true);
      conn.setDoOutput(true);
      conn.setRequestProperty("Host", uri.getHost());
      conn.setRequestProperty("Content-Type", "application/json");
      out = conn.getOutputStream();
      out.write(byteArray);
      out.close();
      if(conn.getResponseCode()==200) {
        rsp = getStreamAsString(conn.getInputStream(), "utf-8");
      }else {
        rsp = getStreamAsString(conn.getErrorStream(), "utf-8");
      }
      
      System.out.println(rsp);

    } catch (Exception e) {
      if(null!=out)
        out.close();
      e.printStackTrace();
      
    }
    
  }
  
  /**
   * getJson
   * 
   */
  private static String getJson() {
    return "{" + "\"name\"" + ":" + "\"robo_blogs_zh123\"" + "}";
  }

  private static String getStreamAsString(InputStream stream, String charset) throws IOException {
    try {
      Reader reader = new InputStreamReader(stream, charset);
      StringBuilder response = new StringBuilder();

      final char[] buff = new char[1024];
      int read = 0;
      while ((read = reader.read(buff)) > 0) {
        response.append(buff, 0, read);
      }

      return response.toString();
    } finally {
      if (stream != null) {
        stream.close();
      }
    }
  }

}

//定制Verifier
class TrustAnyHostnameVerifier implements HostnameVerifier {
  public boolean verify(String hostname, SSLSession session) {
    return true;
  }
}

上一篇:java如何获取byte的高四位和低四位

栏    目:JAVA代码

下一篇:SpringCloud手写Ribbon实现负载均衡

本文标题:Java发送https请求代码实例

本文地址:http://www.codeinn.net/misctech/197375.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有