You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

692 lines
23 KiB

package com.fr.plugin;
import com.fr.stable.StringUtils;
import com.fr.third.org.apache.http.*;
import com.fr.third.org.apache.http.client.*;
import com.fr.third.org.apache.http.client.config.RequestConfig;
import com.fr.third.org.apache.http.client.entity.UrlEncodedFormEntity;
import com.fr.third.org.apache.http.client.methods.CloseableHttpResponse;
import com.fr.third.org.apache.http.client.methods.HttpGet;
import com.fr.third.org.apache.http.client.methods.HttpPost;
import com.fr.third.org.apache.http.client.utils.URIBuilder;
import com.fr.third.org.apache.http.config.Registry;
import com.fr.third.org.apache.http.config.RegistryBuilder;
import com.fr.third.org.apache.http.conn.socket.ConnectionSocketFactory;
import com.fr.third.org.apache.http.conn.socket.PlainConnectionSocketFactory;
import com.fr.third.org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import com.fr.third.org.apache.http.entity.ByteArrayEntity;
import com.fr.third.org.apache.http.impl.client.BasicCookieStore;
import com.fr.third.org.apache.http.impl.client.CloseableHttpClient;
import com.fr.third.org.apache.http.impl.client.HttpClients;
import com.fr.third.org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import com.fr.third.org.apache.http.message.BasicNameValuePair;
import com.fr.third.org.apache.http.util.CharArrayBuffer;
import com.fr.third.org.apache.http.util.EntityUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.*;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpUtils {
private static final Log logger = LogFactory.getLog(HttpUtils.class);
public CookieStore cookieStore = new BasicCookieStore();
private CloseableHttpClient getHttpClientSSL() throws KeyManagementException, NoSuchAlgorithmException {
SSLContext sslcontext = null;
try {
sslcontext = createIgnoreVerifySSL();
} catch (Exception ex) {
//logger.error(ex.getMessage());
}
Registry<ConnectionSocketFactory> socketFactoryRegistry;
if (sslcontext != null)
socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", new SSLConnectionSocketFactory(sslcontext))
.build();
else
socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
HttpClients.custom().setConnectionManager(connManager);
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(30000)
.setConnectTimeout(30000)
.setConnectionRequestTimeout(30000)
.setStaleConnectionCheckEnabled(true)
.build();
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(connManager)
.setDefaultCookieStore(cookieStore)
.setDefaultRequestConfig(defaultRequestConfig)
.build();
return client;
}
private CloseableHttpClient getHttpClient() throws Exception {
SSLContext sslcontext = createIgnoreVerifySSL();
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
HttpClients.custom().setConnectionManager(connManager);
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setStaleConnectionCheckEnabled(true)
.build();
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(connManager)
.setDefaultCookieStore(cookieStore)
.setDefaultRequestConfig(defaultRequestConfig)
.build();
return client;
}
/**
* get请求,参数放在map里
*
* @param url 请求地址
* @param param 参数map
* @return 响应
*/
public String getData(String url, Map<String, String> param, Map<String, String> headers) {
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
URIBuilder builder = new URIBuilder(url);
for (Map.Entry<String, String> entry : param.entrySet()) {
builder.addParameter(entry.getKey(), entry.getValue());
}
HttpGet get = new HttpGet(builder.build());
if (headers != null)
for (Map.Entry<String, String> entry : headers.entrySet()) {
get.setHeader(entry.getKey(), entry.getValue());
}
response = httpClient.execute(get);
if (response != null && response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
result = entityToString(entity);
}
return result;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
if (response != null) {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* get请求,参数放在map里
*
* @param url
* @param param
* @return
*/
public String getData(String url, Map<String, String> param) {
return getData(url, param, null);
}
public String getDataSSL(String url, Map<String, String> param) throws Exception {
return getDataSSL(url, param, null);
}
/**
* SSL协议 get请求,参数放在map里
*
* @param url 请求地址
* @param param 参数map
* @return 响应
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public String getDataSSL(String url, Map<String, String> param, Map<String, String> headers) throws KeyManagementException, NoSuchAlgorithmException {
String result = null;
CloseableHttpClient httpClient = getHttpClientSSL();
CloseableHttpResponse response = null;
try {
URIBuilder builder = new URIBuilder(url);
for (Map.Entry<String, String> entry : param.entrySet()) {
builder.addParameter(entry.getKey(), entry.getValue());
}
HttpGet get = new HttpGet(builder.build());
if (headers != null)
for (Map.Entry<String, String> entry : headers.entrySet()) {
get.setHeader(entry.getKey(), entry.getValue());
}
response = httpClient.execute(get);
if (response != null && response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
result = entityToString(entity);
}
return result;
} catch (Exception e) {
logger.error("请求地址:" + url + ",请求参数:" + param + ",响应数据:" + result);
e.printStackTrace();
} finally {
try {
httpClient.close();
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* SSL协议 get请求,参数放在map里 参数直接拼接在url后面
*
* @param url 请求地址
* @param param 参数map
* @return 响应
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public String getDataSSLUrl(String url, Map<String, String> param, Map<String, String> headers) throws KeyManagementException, NoSuchAlgorithmException {
String result = null;
CloseableHttpClient httpClient = getHttpClientSSL();
CloseableHttpResponse response = null;
try {
StringBuilder stringBuilder = new StringBuilder();
if (param != null)
for (Map.Entry<String, String> entry : param.entrySet()) {
stringBuilder.append(entry.getKey());
if (entry.getValue() != null)
stringBuilder.append("=").append(entry.getValue());
stringBuilder.append("&");
}
if (stringBuilder.length() > 0)
url = addUrlParam(url, stringBuilder.substring(0, stringBuilder.length() - 1));
URIBuilder builder = new URIBuilder(url);
// for(Map.Entry<String,String> entry : param.entrySet())
// {
// builder.addParameter(entry.getKey(),entry.getValue());
// }
//
//
HttpGet get = new HttpGet(builder.build());
if (headers != null)
for (Map.Entry<String, String> entry : headers.entrySet()) {
get.setHeader(entry.getKey(), entry.getValue());
}
response = httpClient.execute(get);
if (response != null && response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
result = entityToString(entity);
}
return result;
} catch (Exception e) {
logger.error("请求地址:" + url + ",请求参数:" + param + ",响应数据:" + result);
e.printStackTrace();
} finally {
try {
httpClient.close();
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 发送post请求,参数用map接收
*
* @param url 地址
* @param param 参数
* @return 返回值
*/
public String postData(String url, Map<String, String> param, Map<String, String> headers) {
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : param.entrySet()) {
pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
CloseableHttpResponse response = null;
try {
post.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8"));
if (headers != null)
for (Map.Entry<String, String> entry : headers.entrySet()) {
post.setHeader(entry.getKey(), entry.getValue());
}
response = httpClient.execute(post);
if (response != null && response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
result = entityToString(entity);
}
return result;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 发送post请求,参数用map接收
*
* @param url 地址
* @param param 参数
* @return 返回值
*/
public String postData(String url, Map<String, String> param) {
return postData(url, param, null);
}
/**
* SSL协议发送post请求,参数用map接收
*
* @param url 地址
* @param param 参数
* @return 返回值
*/
public String postDataSSL(String url, Map<String, String> param) throws KeyManagementException, NoSuchAlgorithmException {
return postDataSSL(url, param, null);
}
/**
* SSL协议发送post请求,参数用map接收
*
* @param url 地址
* @param param 参数
* @return 返回值
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public String postDataSSL(String url, Map<String, String> param, Map<String, String> headers) throws KeyManagementException, NoSuchAlgorithmException {
String result = null;
CloseableHttpClient httpClient = getHttpClientSSL();
HttpPost post = new HttpPost(url);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : param.entrySet()) {
pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
CloseableHttpResponse response = null;
try {
post.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8"));
if (headers != null)
for (Map.Entry<String, String> entry : headers.entrySet()) {
post.setHeader(entry.getKey(), entry.getValue());
}
response = httpClient.execute(post);
if (response != null && response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
result = entityToString(entity);
}
return result;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* post请求,参数为json字符串
*
* @param url 请求地址
* @param jsonString json字符串
* @return 响应
*/
public String postJsonData(String url, String jsonString) {
return postJsonData(url, jsonString, null);
}
/**
* post请求,参数为json字符串
*
* @param url 请求地址
* @param jsonString json字符串
* @return 响应
*/
public String postJsonData(String url, String jsonString, Map<String, String> headers) {
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
CloseableHttpResponse response = null;
try {
post.setEntity(new ByteArrayEntity(jsonString.getBytes("UTF-8")));
post.setHeader("Content-type", "application/json");
if (headers != null)
for (Map.Entry<String, String> entry : headers.entrySet()) {
post.setHeader(entry.getKey(), entry.getValue());
}
response = httpClient.execute(post);
if (response != null && response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
result = entityToString(entity);
}
return result;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* post请求,参数为json字符串
*
* @param url 请求地址
* @param jsonString json字符串
* @return 响应
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public String postJsonDataSSL(String url, String jsonString, Map<String, String> headers) throws KeyManagementException, NoSuchAlgorithmException {
String result = null;
CloseableHttpClient httpClient = getHttpClientSSL();
HttpPost post = new HttpPost(url);
CloseableHttpResponse response = null;
try {
post.setEntity(new ByteArrayEntity(jsonString.getBytes("UTF-8")));
post.setHeader("Content-type", "application/json");
if (headers != null)
for (Map.Entry<String, String> entry : headers.entrySet()) {
post.setHeader(entry.getKey(), entry.getValue());
}
response = httpClient.execute(post);
if (response != null && response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
result = entityToString(entity);
}
return result;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
private String entityToString(HttpEntity entity) throws IOException {
String result = null;
if (entity != null) {
long lenth = entity.getContentLength();
if (lenth != -1 && lenth < 2048) {
result = EntityUtils.toString(entity, "UTF-8");
} else {
InputStreamReader reader1 = new InputStreamReader(entity.getContent(), "UTF-8");
CharArrayBuffer buffer = new CharArrayBuffer(2048);
char[] tmp = new char[1024];
int l;
while ((l = reader1.read(tmp)) != -1) {
buffer.append(tmp, 0, l);
}
result = buffer.toString();
}
}
return result;
}
/**
* 绕过验证
*
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
SSLContext sc = SSLContext.getInstance("SSLv3");
// 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
X509TrustManager trustManager = new X509TrustManager() {
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
String paramString) throws CertificateException {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
String paramString) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sc.init(null, new TrustManager[]{trustManager}, null);
return sc;
}
private static byte[] getbytes(InputStream is) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
byte[] b = new byte[2048];
int n = 0;
while ((n = is.read(b)) != -1) {
bos.write(b, 0, n);
}
return bos.toByteArray();
} catch (Exception e) {
} finally {
try {
if (is != null) {
is.close();
}
} catch (Exception e) {
}
try {
if (bos != null) {
bos.close();
}
} catch (Exception e) {
}
}
return null;
}
/**
* 根据url下载文件,保存到filepath中
*
* @param url
* @param param
* @return
*/
public boolean downloadFile(String url, OutputStream stream, Map<String, String> param, Map<String, String> headers) {
try {
CloseableHttpClient httpClient = getHttpClientSSL();
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpClient.execute(httpget);
//info.setResult(result);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
byte[] buffer = new byte[10 * 1024];
int ch = 0;
while ((ch = is.read(buffer)) != -1) {
stream.write(buffer, 0, ch);
}
is.close();
stream.flush();
stream.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 根据url下载文件,保存到filepath中
*
* @param url
* @param filepath
* @return
*/
public boolean downloadFile(String url, String filepath, Map<String, String> param, Map<String, String> headers) {
try {
CloseableHttpClient httpClient = getHttpClientSSL();
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
//String str=response.getHeaders("Content-disposition");
File file = new File(filepath);
file.getParentFile().mkdirs();
FileOutputStream fileout = new FileOutputStream(file);
byte[] buffer = new byte[10 * 1024];
int ch = 0;
while ((ch = is.read(buffer)) != -1) {
fileout.write(buffer, 0, ch);
}
is.close();
fileout.flush();
fileout.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 链接地址后追加参数
*/
public static String addUrlParam(String url, String... param) {
if (param != null) {
for (String str : param)
if (StringUtils.isNotBlank(str)) {
if (url.indexOf("?") >= 0) {
url += "&" + str;
} else {
url += "?" + str;
}
}
}
return url;
}
}