All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.founder.fsi.utils.HttpUtils Maven / Gradle / Ivy
package com.founder.fsi.utils;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import com.founder.core.log.MyLog;
import org.apache.commons.io.IOUtils;
import org.springframework.http.HttpHeaders;
public class HttpUtils {
private static final MyLog LOG = MyLog.getLog(HttpUtils.class);
private static String PROXY_HOST = "";
private static String PROXY_PORT = "";
public static byte[] post(String requestUrl, String outputStr, int connectTimeout, int readTimeout, HttpHeaders httpHeaders, boolean isFile) {
try {
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new SecureRandom());
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpURLConnection httpUrlConn;
if (PROXY_HOST.length() > 0) {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_HOST, Integer.valueOf(PROXY_PORT).intValue()));
httpUrlConn = (HttpURLConnection)url.openConnection(proxy);
} else {
httpUrlConn = (HttpURLConnection)url.openConnection();
}
InputStream inputStream = new ByteArrayInputStream(new byte[]{});
try {
if ((httpUrlConn instanceof HttpsURLConnection)) {
HttpsURLConnection httpsConn = (HttpsURLConnection)httpUrlConn;
httpsConn.setSSLSocketFactory(ssf);
}
httpUrlConn.setRequestProperty("Charset", "UTF-8");
if (isFile) {
httpUrlConn.setRequestProperty("Content-Type", "application/octet-stream");
httpUrlConn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
} else {
httpUrlConn.setRequestProperty("Content-type", "application/json");
}
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
httpUrlConn.setReadTimeout(readTimeout);
httpUrlConn.setConnectTimeout(connectTimeout);
if (httpHeaders.containsKey("x-tif-paasid")) {
httpUrlConn.setRequestProperty("x-tif-paasid", httpHeaders.getFirst("x-tif-paasid"));
}
if (httpHeaders.containsKey("x-tif-signature")) {
httpUrlConn.setRequestProperty("x-tif-signature", httpHeaders.getFirst("x-tif-signature"));
}
if (httpHeaders.containsKey("x-tif-timestamp")) {
httpUrlConn.setRequestProperty("x-tif-timestamp", httpHeaders.getFirst("x-tif-timestamp"));
}
if (httpHeaders.containsKey("x-tif-nonce")) {
httpUrlConn.setRequestProperty("x-tif-nonce", httpHeaders.getFirst("x-tif-nonce"));
}
if (httpHeaders.containsKey("x-rio-paasid")) {
httpUrlConn.setRequestProperty("x-rio-paasid", httpHeaders.getFirst("x-rio-paasid"));
}
if (httpHeaders.containsKey("x-rio-signature")) {
httpUrlConn.setRequestProperty("x-rio-signature", httpHeaders.getFirst("x-rio-signature"));
}
if (httpHeaders.containsKey("x-rio-timestamp")) {
httpUrlConn.setRequestProperty("x-rio-timestamp", httpHeaders.getFirst("x-rio-timestamp"));
}
if (httpHeaders.containsKey("x-rio-nonce")) {
httpUrlConn.setRequestProperty("x-rio-nonce", httpHeaders.getFirst("x-rio-nonce"));
}
httpUrlConn.setRequestMethod("POST");
if (null != outputStr) {
OutputStream outputStream = httpUrlConn.getOutputStream();
outputStream.write(outputStr.getBytes(StandardCharsets.UTF_8));
outputStream.close();
}
int code = httpUrlConn.getResponseCode();//判断响应码getResponseCode()不是200,201,202的话,使用getErrorStream()而不是直接getInputStream()
LOG.info("打印响应码:" + code); //https://www.it610.com/article/1295747089854177280.htm
if (code == HttpURLConnection.HTTP_OK || code == HttpURLConnection.HTTP_CREATED || code == HttpURLConnection.HTTP_ACCEPTED) {
inputStream = httpUrlConn.getInputStream();
} else {
inputStream = httpUrlConn.getErrorStream();
byte[] buffer = IOUtils.toByteArray(inputStream);
String result = IOUtils.toString(buffer,"UTF-8");
throw new RuntimeException(result);
}
return IOUtils.toByteArray(inputStream);
} finally {
inputStream.close();
httpUrlConn.disconnect();
}
} catch (Exception e) {
LOG.error("http request error url:" + requestUrl, e);
throw new RuntimeException("http request error url:" + requestUrl + "," + e.getMessage());
}
}
private static class MyX509TrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] chain, String authType) {}
public void checkServerTrusted(X509Certificate[] chain, String authType) {}
public X509Certificate[] getAcceptedIssuers()
{
return null;
}
}
}