导航

一、java发送http的各类请求

二、java发送https的各类请求

java开发中需要调用其他服务的对外提供的https请求,上一篇写的是调用http,处理方式不太一样,可以参考如下代码:

注:调用的主类比较简单就不写了。

pom.xml

        <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.10</version>
</dependency> <dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
HttpsUtil.class类
package cn.cas.xjipc.yjsapplication.unit;

import org.apache.commons.httpclient.util.HttpURLConnection;
import org.springframework.stereotype.Component; import java.io.*;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; import javax.net.ssl.*; @Component
public class HttpsUtil { private static class TrustAnyTrustManager implements X509TrustManager { public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
} public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
} public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
} private static class TrustAnyHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
} /**
* post方式请求服务器(https协议)
*
* @param url 请求地址
* @param content 参数
* @param charset 编码
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
* @throws IOException
*/
public String post(String url, String content, String charset)
throws NoSuchAlgorithmException, KeyManagementException,
IOException {
String result = "";
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[]{new TrustAnyTrustManager()},
new java.security.SecureRandom()); URL console = new URL(url);
HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
conn.setSSLSocketFactory(sc.getSocketFactory());
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
conn.setDoOutput(true);
conn.connect();
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.write(content.getBytes(charset));
// 刷新、关闭
out.flush();
out.close();
InputStream is = conn.getInputStream();
if (is != null) {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
is.close();
byte[] array = outStream.toByteArray();
result = new String(array, "utf-8");
return result;
}
return null;
} /**
* put方式请求服务器(https协议)
*
* @param url 请求地址
* @param content 参数
* @param token 编码
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
* @throws IOException
*/
public String put(String url, String content, String token)
throws NoSuchAlgorithmException, KeyManagementException,
IOException {
String result = "";
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[]{new TrustAnyTrustManager()},
new java.security.SecureRandom()); URL console = new URL(url);
HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
conn.setRequestMethod("PUT");
conn.setSSLSocketFactory(sc.getSocketFactory());
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
//conn.setRequestProperty("Authorization", "xxxxx" + token);
conn.connect();
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
//out.write(content.getBytes("UTF8"));
// 刷新、关闭
out.flush();
out.close();
InputStream is = conn.getInputStream();
if (is != null) {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
is.close(); byte[] array = outStream.toByteArray();
result = new String(array, "utf-8");
return result;
}
return null;
} /**
* get方式请求服务器(https协议)
*
* @param url 请求地址
* @param content 参数
* @param token 编码
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
* @throws IOException
*/
public String get(String url, String content, String token)
throws NoSuchAlgorithmException, KeyManagementException,
IOException {
String result = "";
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[]{new TrustAnyTrustManager()},
new java.security.SecureRandom()); URL httpUrl = new URL(url);
HttpsURLConnection conn = (HttpsURLConnection) httpUrl.openConnection();
conn.setRequestMethod("GET");
conn.setSSLSocketFactory(sc.getSocketFactory());
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
//conn.setRequestProperty("Authorization", "xxxxxx" + token);
conn.connect();

//get方法与post方法除了conn.setRequestMethod("GET")这句不一样外,关于DataOutputStream out的几行一定要删除,否则就会报405的错误 InputStream is = conn.getInputStream();
if (is != null) {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
is.close(); byte[] array = outStream.toByteArray();
result = new String(array, "utf-8");
return result;
} return null;
} }

推荐一个适合零基础学习SQL的网站:不用安装数据库,在线轻松学习SQL!

二、java发送https的各类请求的更多相关文章

  1. Java发送HTTPS请求

    前言 上篇文章介绍了 java 发送 http 请求,大家都知道发送http是不安全的 .我也是由于对接了其他企业后总结了一套发送 https的工具.大家网上找方法很多的,但是可不是你粘过来就能用啊, ...

  2. Java 发送 Https 请求工具类 (兼容http)

    依赖 jsoup-1.11.3.jar <dependency> <groupId>org.jsoup</groupId> <artifactId>js ...

  3. Java发送http get/post请求,调用接口/方法

    由于项目中要用,所以找了一些资料,整理下来. GitHub地址: https://github.com/iamyong    转自:http://blog.csdn.net/capmiachael/a ...

  4. JAVA发送http GET/POST请求的两种方式+JAVA http 请求手动配置代理

    java发送http get请求,有两种方式. 第一种用URLConnection: public static String get(String url) throws IOException { ...

  5. Java 发送http GET/POST请求

    最近项目里面需要用到Java发送http请求,由于发送https请求有点复杂,暂时不考虑 HttpURLConnection HttpURLConnection是一种多用途.轻量极的HTTP客户端,使 ...

  6. Java发送get及post请求工具方法

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...

  7. JAVA发送http get/post请求,调用http接口、方法

    import java.io.BufferedReader; import java.io.IOException;import java.io.InputStream; import java.io ...

  8. Java 发送Get和Post请求

    package com.htpt.superviseServices.dm.util; import java.io.BufferedReader; import java.io.IOExceptio ...

  9. java的https的get请求

    package com.wl.webservice; import java.io.InputStream; import java.net.HttpURLConnection; import jav ...

  10. java发送GET和post请求

    package com.baqingshe.bjs.util; import java.io.BufferedReader; import java.io.IOException; import ja ...

随机推荐

  1. 用Spring导致的无法运行Java文件的问题的解决方案

    一般来说,这个问题主要是出现在用idea社区版的Spring Initializr这个插件来创建Spring项目.接下来我会以图的方式进行解说. 第一步创建Spring项目: 这里用maven的方式进 ...

  2. [ABC262A] World Cup

    Problem Statement A sport event is held in June of every year whose remainder when divided by $4$ is ...

  3. MybatisPlus条件查询方法全解

    1.是什么? MybatisPlus通过条件构造器可以组装复杂的查询条件,写一些复杂的SQL语句,从而简化我们的开发提升我们的开发效率 # 可以简单的理解为就是我们写SQL语句时where后面的条件 ...

  4. 【Python微信机器人】第六篇:优化使用方式,可pip安装

    优化内容 这篇不聊技术点,说一下优化后的Python机器人代码怎么使用,优化内容如下: 将hook库独立成一个库,发布到pypi,可使用pip安装 将微信相关的代码发布成另一个库,也可以pip安装 g ...

  5. Oracle参数文件spfile

    spfile:server parameter file. spfile只能通过OEM(oracle enterprise manager)软件或者alter system命令进行修改. spfile ...

  6. mysql将查询结果生成临时表

    MySQL中将查询的结果生成临时表,列类型与查询的列一致,百度搜索到的没啥用. 直接上SQL: 将结果生成临时表 create temporary table temp_tb_name as (sel ...

  7. windows环境下如何优雅搭建ftp服务?

    目录 0. 前言 1.ftp简介 2.下载Apache FTPServer 3.下载并解压压缩包 4.修改配置文件 4.1 修改users.properties配置文件 4.2 修改ftpd-typi ...

  8. Java 插入、隐藏/显示、删除Excel行或列

    概述 操作Excel工作表时,对表格中的行或列数据可执行,包括插入.隐藏.显示.删除等在内的多种操作需求,本文将通过Java代码示例演示每种操作的具体实现方法.文中方法使用了Java Excel类库( ...

  9. RDS:一致性处理事务的神器

    摘要:RDS关系型数据库是一种基于云计算平台的即开即用.稳定可靠.弹性伸缩.便捷管理的在线关系型数据库服务. 本文分享自华为云社区<一致性处理事务这下还是看RDS的吧[秋招特训]>,作者: ...

  10. vue2升级vue3:getCurrentInstance—Composition api/hooks中如何获取$el

    在vue2中,我们进程看到 this.$el 操作.但是在vue3 如何获取组件的当前 dom 元素呢?  可以利用 getCurrentInstance getCurrentInstance Vue ...