导航

一、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. python3 打包上传pypi失败及解决方法

    1.打包及上传 1.1.安装构建和打包工具 pip3 install build # 构建包的工具 pip3 install twine # 上传包的工具 pip3 install wheel #he ...

  2. Fast ORM 读写分离功能使用方式

    Fast Framework 作者 Mr-zhong 代码改变世界.... 一.前言 Fast Framework 基于NET6.0 封装的轻量级 ORM 框架 支持多种数据库 SqlServer O ...

  3. Netty内置的http报文解码流程

    netty解码 netty通过内置处理器HttpRequestDecoder和HttpObjectAggregator对Http请求报文进行解码之后,Netty会将Http请求封装成一个FullHtt ...

  4. 一款开源免费美观的WinForm UI控件库 - ReaLTaiizor

    前言 今天推荐一款基于MIT license开源.免费.美观的.NET WinForm UI控件库:ReaLTaiizor. 什么是WinForm? WinForm是一个传统的桌面应用程序框架,它基于 ...

  5. LeetCode:不用加号的加法(位运算)

    解题思路:位运算,只能用位运算符.a.b同号比较好处理.主要是异号的情况,考虑 a>0,b<0,因为 a,b的绝对值都不会超过2^32,因此取模数为2^32.根据同余方程可知 (a+b)% ...

  6. Python——第四章:函数的递归调用

    递归:  函数自己调用自己 递归如果没有任何东西拦截的话. 它默认就是一个死循环 def func() func() func() 因此递归调用的时候需要有判断,来退出循环 def func() if ...

  7. Android对接华为AI - 文本识别

    准备工作 在开发应用前: 1.需要在AppGallery Connect中配置相关信息,包括:注册成为开发者和创建应用. 2.使用ML Kit云侧服务(端侧服务可不开通)需要开发者在AppGaller ...

  8. 未经授权访问 .js

    流程顺序:后台管理登陆地址 → 后台主页地址 → fuzz测试出用户管理列表接口 → 直接调接口..全程黑盒. 那么接下来我逆着来推理下逻辑: 首先是拿到某后台管理登录的网址 接着查看html源码,发 ...

  9. curl使用小记(二)——远程下载一张图片

    目录 1. 概述 2. 实例 3. 参考 1. 概述 在之前的文章<curl使用小记(一)>中论述了命令行工具curl的基本使用.除此之外,curl还提供了能够直接供程序调用的模块库接口l ...

  10. 日常Bug排查-集群逐步失去响应

    前言 日常Bug排查系列都是一些简单Bug排查.笔者将在这里介绍一些排查Bug的简单技巧,同时顺便积累素材_ Bug现场 最近碰到一个产线问题,表现为某个应用集群所有的节点全部下线了.导致上游调用全部 ...