Java如何实现发送HTTPS请求的代码示例?

2026-06-10 16:041阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计301个文字,预计阅读时间需要2分钟。

Java如何实现发送HTTPS请求的代码示例?

1. 前文:通过webService发送HTTPS请求,有两种版本,一种是携带证书验证(比较繁琐),另一种是直接忽略证书,文本提供的即为第二种(已测试过)。

2. 最简代码: java import j;

1、前文:通过webService发送*****"; String url = "*****"; 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如何实现发送HTTPS请求的代码示例?

本文共计301个文字,预计阅读时间需要2分钟。

Java如何实现发送HTTPS请求的代码示例?

1. 前文:通过webService发送HTTPS请求,有两种版本,一种是携带证书验证(比较繁琐),另一种是直接忽略证书,文本提供的即为第二种(已测试过)。

2. 最简代码: java import j;

1、前文:通过webService发送*****"; String url = "*****"; 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如何实现发送HTTPS请求的代码示例?