144 lines
4.6 KiB
Java
144 lines
4.6 KiB
Java
package com.yunhai.userbase.utils;
|
|
|
|
import com.bestsch.utils.DException;
|
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
import javax.net.ssl.SSLContext;
|
|
import javax.net.ssl.TrustManager;
|
|
import javax.net.ssl.X509TrustManager;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.nio.ByteBuffer;
|
|
import java.security.KeyManagementException;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.cert.X509Certificate;
|
|
|
|
public class HttpUtils {
|
|
static {
|
|
try {
|
|
trustAllHttpsCertificates();
|
|
HttpsURLConnection.setDefaultHostnameVerifier
|
|
(
|
|
(urlHostName, session) -> true
|
|
);
|
|
} catch (Exception e) {
|
|
}
|
|
}
|
|
/**
|
|
* 跳过ssl证书
|
|
*
|
|
* @throws NoSuchAlgorithmException
|
|
* @throws KeyManagementException
|
|
*/
|
|
private static void trustAllHttpsCertificates()
|
|
throws NoSuchAlgorithmException, KeyManagementException {
|
|
TrustManager[] trustAllCerts = new TrustManager[1];
|
|
trustAllCerts[0] = (TrustManager) new TrustAllManager();
|
|
SSLContext sc = SSLContext.getInstance("SSL");
|
|
sc.init(null, trustAllCerts, null);
|
|
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
|
|
}
|
|
|
|
private static class TrustAllManager implements X509TrustManager {
|
|
public X509Certificate[] getAcceptedIssuers() {
|
|
return null;
|
|
}
|
|
|
|
public void checkServerTrusted(X509Certificate[] certs, String authType) {
|
|
}
|
|
|
|
public void checkClientTrusted(X509Certificate[] certs, String authType) {
|
|
}
|
|
}
|
|
|
|
public static String get(String aUrl,Integer timeout){
|
|
int respCode = 0;
|
|
HttpURLConnection conn = null;
|
|
String var3;
|
|
try {
|
|
conn = getHttpURLConnection(aUrl, "GET",timeout);
|
|
conn.setDoInput(true);
|
|
respCode = conn.getResponseCode();
|
|
if (respCode != 301) {
|
|
InputStream is = conn.getInputStream();
|
|
int contentLen = conn.getContentLength();
|
|
byte[] resultData = read(is, contentLen);
|
|
is.close();
|
|
String var6 = new String(resultData, "utf-8");
|
|
return var6;
|
|
}
|
|
var3 = get(conn.getHeaderField("Location"),timeout);
|
|
} catch (Exception var10) {
|
|
throw new DException("网络连接失败");
|
|
} finally {
|
|
close(conn);
|
|
}
|
|
return var3;
|
|
}
|
|
|
|
|
|
private static HttpURLConnection getHttpURLConnection(String uri, String method,int timeout) throws IOException {
|
|
URL url = new URL(uri);
|
|
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
|
conn.setRequestMethod(method);
|
|
conn.setConnectTimeout(timeout);
|
|
conn.setReadTimeout(timeout);
|
|
return conn;
|
|
}
|
|
|
|
private static byte[] read(InputStream is, int contentLen) throws IOException {
|
|
if (contentLen <= 0) {
|
|
int BUF_SIZE = 102400;
|
|
byte[] buf = new byte[102400];
|
|
ByteBuffer byteBuffer = ByteBuffer.allocate(102400);
|
|
|
|
int readed;
|
|
do {
|
|
if (byteBuffer.position() + 102400 > byteBuffer.capacity()) {
|
|
byteBuffer = reallocByteBuffer(byteBuffer, byteBuffer.capacity() + 102400);
|
|
}
|
|
|
|
readed = is.read(buf, 0, 102400);
|
|
if (readed > 0) {
|
|
byteBuffer.put(buf, 0, readed);
|
|
}
|
|
} while(readed != -1);
|
|
|
|
byte[] data = new byte[byteBuffer.position()];
|
|
System.arraycopy(byteBuffer.array(), 0, data, 0, byteBuffer.position());
|
|
return data;
|
|
} else {
|
|
byte[] data = new byte[contentLen];
|
|
|
|
int readed;
|
|
for(int remain = contentLen; remain > 0; remain -= readed) {
|
|
readed = is.read(data, contentLen - remain, remain);
|
|
if (readed == -1) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
return data;
|
|
}
|
|
}
|
|
private static ByteBuffer reallocByteBuffer(ByteBuffer buffer, int size) {
|
|
ByteBuffer newBuf = ByteBuffer.allocate(size);
|
|
newBuf.put(buffer.array(), 0, buffer.position());
|
|
return newBuf;
|
|
}
|
|
private static void close(HttpURLConnection conn) {
|
|
try {
|
|
if (conn == null) {
|
|
return;
|
|
}
|
|
|
|
conn.disconnect();
|
|
} catch (Exception var2) {
|
|
var2.printStackTrace();
|
|
}
|
|
|
|
}
|
|
}
|