[转] java实现https请求
2024-08-30 05:50:13
package com.lichmama.test.util; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager; public class HttpsUtil { private static final class DefaultTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
} @Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
} @Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
} private static HttpsURLConnection getHttpsURLConnection(String uri, String method) throws IOException {
SSLContext ctx = null;
try {
ctx = SSLContext.getInstance("TLS");
ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
SSLSocketFactory ssf = ctx.getSocketFactory(); URL url = new URL(uri);
HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection();
httpsConn.setSSLSocketFactory(ssf);
httpsConn.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
});
httpsConn.setRequestMethod(method);
httpsConn.setDoInput(true);
httpsConn.setDoOutput(true);
return httpsConn;
} private static byte[] getBytesFromStream(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] kb = new byte[1024];
int len;
while ((len = is.read(kb)) != -1) {
baos.write(kb, 0, len);
}
byte[] bytes = baos.toByteArray();
baos.close();
is.close();
return bytes;
} private static void setBytesToStream(OutputStream os, byte[] bytes) throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
byte[] kb = new byte[1024];
int len;
while ((len = bais.read(kb)) != -1) {
os.write(kb, 0, len);
}
os.flush();
os.close();
bais.close();
} public static byte[] doGet(String uri) throws IOException {
HttpsURLConnection httpsConn = getHttpsURLConnection(uri, "GET");
return getBytesFromStream(httpsConn.getInputStream());
} public static byte[] doPost(String uri, String data) throws IOException {
HttpsURLConnection httpsConn = getHttpsURLConnection(uri, "POST");
setBytesToStream(httpsConn.getOutputStream(), data.getBytes());
return getBytesFromStream(httpsConn.getInputStream());
}
}
下载个文件(bing今日美图)测试下:
public class TestHttps { public static void main(String[] args) throws IOException {
String uri = "https://cn.bing.com/hpwp/356677bea977a9aa166a92ab94848f17";
byte[] bytes = HttpsUtil.doGet(uri);
FileOutputStream fos = new FileOutputStream("D:/bing.picture-of-day.jpg");
fos.write(bytes);
fos.close();
System.out.println("done!");
}
}
在weblogic中使用如上代码时,可能会出现ClassCastException,详情及解决方案可查看以下链接:
http://www.th7.cn/Program/java/201511/688262.shtml
http://blog.csdn.net/arvinrong/article/details/7715334[
最新文章
- C# - 多线程 之 异步编程
- React Native常用组件Image使用
- <;Oracle Database>;数据库启动与关闭
- iOS7 中的statusbar的隐藏和样式更改
- 三,samba
- insertorupdate
- 如何开发 Grunt 插件
- 【转】 C语言自增自减运算符深入剖析
- (原)调用jpeglib对图像进行压缩
- C 中注意的小问题
- jquery validate 插件使用小结
- SQL server存储过程:数据的插入和更新
- markdown 常用格式API
- C#方法中参数ref和out的解析
- Elixir的Phoenix框架:请求处理之道
- flex 布局 input 宽度不自适应
- hdu5651 xiaoxin juju needs help(逆元)
- java基础语法3
- Applet web端对文件的读取方式
- Quartz.Net进阶之六:详述 JobStores
热门文章
- 洛谷 P1496 火烧赤壁
- select查询语句执行顺序
- CF285 E Positions in Permutations——“恰好->;大于”的容斥和允许“随意放”的dp
- POJ3067(树状数组:统计数字出现个数)
- 【转】Pro Android学习笔记(六):了解Content Provider(中)
- Python list的定义和删改
- java&;nbsp;POP3
- 《精通Spring4.X企业应用开发实战》读后感第四章(资源访问)
- 2. nmap扫描神器总结
- Kth Largest Element in a Stream