导航

一、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. 如何自学 PS、PR 等软件?

    学习Photoshop(PS)和Premiere Pro(PR)等软件需要一定的时间和耐心,以下是非常详细的自学指南. 第一部分:规划学习路线 1. 确定学习目标 在自学PS和PR之前,首先要明确自己 ...

  2. SpringCore完整学习教程5,入门级别

    本章从第6章开始 6. JSON Spring Boot提供了三个JSON映射库的集成: Gson Jackson JSON-B Jackson是首选的和默认的库. 6.1. Jackson 为Jac ...

  3. Tensorflow2.0使用Resnet18进行数据训练

    在今年的3月7号,谷歌在 Tensorflow Developer Summit 2019 大会上发布 TensorFlow 2.0 Alpha 版,随后又发布了Beta版本. Resnet18结构 ...

  4. 华企盾DSC:wps个人模式无策略组新建的文件仍然加密

    解决方法:右键wps安装目录手动解密即可(原因:wps模板被加密导致)

  5. DBeaver连接国产数据库OceanBase,以及Python连接,解决ModuleNotFoundError: No module named '_jpype'

    DBeaver连接OceanBase 参考:https://www.modb.pro/db/365929 用户名的格式为: 数据库用户名@租户名#集群名 Python连接OceanBase 参考:ht ...

  6. grpc是基于http/2协议的高性能的rpc框架

    师傅领进门,修行在个人,跟着官方脚手架demo了grpc后,之后就需要扩展前后知识边界,下面总结grpc的前世今生和最佳实践. https://www.cnblogs.com/JulianHuang/ ...

  7. Pikachu漏洞靶场 Burte Force(暴力破解)

    Burte Force(暴力破解) 文章目录 Burte Force(暴力破解) 概述 1.基于表单的暴力破解 2.验证码绕过(on server) 3.验证码绕过(on client) 4.toke ...

  8. win10安装WSL

    一.什么是WSL? Windows Subsystem for Linux 简称 WSL,是一个在Windows 10上能够运行原生Linux二进制可执行文件(ELF格式)的兼容层. 二.如何安装WS ...

  9. 如何在IIS上部署docsify以及404问题

    操作步骤 创建一个文件夹,在文件夹中新建2个文件 index.html:入口文件,整个网站只需要这个html文件,其他文件都是md文件 README.md:主页内容,如果没有这个文件,访问时提示404 ...

  10. 文心一言 VS 讯飞星火 VS chatgpt (63)-- 算法导论6.5 2题

    文心一言 VS 讯飞星火 VS chatgpt (63)-- 算法导论6.5 2题 二.试说明 MAX-HEAP-INSERT(A,10)在堆A=(15,13,9,5,12,8,7,4,0,6,2,1 ...