在请求https的时候,如果没有特别要求握手,数据签名啥的高安全操作,仅仅是用https协议,那么最简单的方法是信任它,不需要加载任何证书。
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
/**
* Created by Leehom on 2017/11/27.
*/
public class HttpsClient {
private static final int CONNECT_TIME_OUT = 15000;
private static final int READ_TIME_OUT = 15000;
private static final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}};
private static final HostnameVerifier NOT_VERYFY = new HostnameVerifier() {
@Override
public boolean verify(String s, SSLSession sslSession) {
return true;
}
};
/**
* 发起http请求并获取结果
*
* @param spec 请求地址
* @param method 请求方式(GET、POST)
* @param data 提交的数据
* @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
*/
public static HttpResult httpRequest(String spec, String method, String data) {
try {
URL url = new URL(spec);
//如果地址是https开头,这里返回的HttpURLConnection实例其实是HttpURLConnection
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setConnectTimeout(CONNECT_TIME_OUT);
http.setReadTimeout(READ_TIME_OUT);
http.setDoInput(true);
if (data != null && data.length() > 0) {
http.setDoOutput(true);
http.setRequestMethod(method);
OutputStream out = http.getOutputStream();
out.write(data.toString().getBytes());
out.flush()
}
http.connect();
int code = http.getResponseCode();
if (code == 200) {
InputStream in = http.getInputStream();
byte[] buf = new byte[512];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int actual = in.read(buf);
while (actual != -1) {
bos.write(buf, 0, actual);
actual = in.read(buf);
}
in.close();
in = null;
bos.flush();
HttpResult result = new HttpResult(code, bos.toByteArray());
bos.close();
bos = null;
return result;
}
} catch (IOException e) {
e.printStackTrace();
}
return new HttpResult();
}
/**
* 发起https请求并获取结果
*
* @param spec 请求地址
* @param method 请求方式(GET、POST)
* @param data 提交的数据
* @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
*/
public static HttpResult httpsRequest(String spec, String method, String data) {
// Create a trust manager that does not validate certificate chains
// Install the all-trusting trust manager
try {// 注意这部分一定要
HttpsURLConnection.setDefaultHostnameVerifier(NOT_VERYFY);
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
return httpRequest(spec, method, data);
} catch (Exception e) {
e.printStackTrace();
}
return new HttpResult();
}
} 收藏的用户(0) X
正在加载信息~
推荐阅读
最新回复 (0)
站点信息
- 文章2314
- 用户1336
- 访客11799182
每日一句
It's the perfect time for a good book by the window.
这是在窗边读一本好书的完美时节。
这是在窗边读一本好书的完美时节。
【转载】cocos2d-x 3.0 制作横版格斗游戏
IntelliJ IDEA2018~2019.1激活码-注册码
转载一篇微信WEB端接口说明
热血传奇,梦开始的地方!
Android Studio3.0引入第三方库文件的总结
升级后Android Studio 3.2.1的编译问题解决办法
Galaxy X:三星的可折叠手机必须向中兴通讯学习
ndk神奇问题之non-numeric second argument to `wordlist' function: '8.7z'
Android Studio开发之app:transformJackWithJackForDebug错误
Java开发人员的7种最佳测试框架
P2P中NAT之间的打洞可能性
linux下C/C++网络编程基本:socket实现tcp和udp的例子
Linux(Centos7)系统下给已有分区进行扩容
新会员