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.
238 lines
8.3 KiB
238 lines
8.3 KiB
3 years ago
|
package com.fr.plugin.sso.utils;
|
||
|
|
||
|
import com.fr.log.FineLoggerFactory;
|
||
|
import com.fr.third.org.apache.http.HttpEntity;
|
||
|
import com.fr.third.org.apache.http.HttpResponse;
|
||
|
import com.fr.third.org.apache.http.HttpStatus;
|
||
|
import com.fr.third.org.apache.http.NameValuePair;
|
||
|
import com.fr.third.org.apache.http.client.CookieStore;
|
||
|
import com.fr.third.org.apache.http.client.entity.UrlEncodedFormEntity;
|
||
|
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.conn.ssl.NoopHostnameVerifier;
|
||
|
import com.fr.third.org.apache.http.entity.StringEntity;
|
||
|
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.cookie.BasicClientCookie;
|
||
|
import com.fr.third.org.apache.http.message.BasicNameValuePair;
|
||
|
import com.fr.third.org.apache.http.ssl.SSLContexts;
|
||
|
import com.fr.third.org.apache.http.ssl.TrustStrategy;
|
||
|
import com.fr.third.org.apache.http.util.EntityUtils;
|
||
|
|
||
|
import javax.net.ssl.SSLContext;
|
||
|
import javax.servlet.http.Cookie;
|
||
|
import java.io.UnsupportedEncodingException;
|
||
|
import java.security.KeyManagementException;
|
||
|
import java.security.KeyStoreException;
|
||
|
import java.security.NoSuchAlgorithmException;
|
||
|
import java.security.cert.CertificateException;
|
||
|
import java.security.cert.X509Certificate;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.List;
|
||
|
import java.util.Map;
|
||
|
import java.util.Set;
|
||
|
|
||
|
public class HttpUtils {
|
||
|
/**
|
||
|
* httpGet请求
|
||
|
* @param url
|
||
|
* @return
|
||
|
*/
|
||
|
public static String get(String url,Cookie[] cookies,Map<String,String> header){
|
||
|
FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--url:"+url);
|
||
|
|
||
|
//创建httpClient
|
||
|
CloseableHttpClient httpclient = createHttpClient(cookies);
|
||
|
|
||
|
HttpGet getMethod = new HttpGet(url);
|
||
|
FRUtils.FRLogInfo("header:"+header.toString());
|
||
|
if(header != null && header.size() > 0){
|
||
|
Set<String> keySet = header.keySet();
|
||
|
|
||
|
for(String key : keySet){
|
||
|
getMethod.setHeader(key,header.get(key));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
HttpResponse response = httpclient.execute(getMethod);
|
||
|
|
||
|
FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--status:"+response.getStatusLine().getStatusCode());
|
||
|
|
||
|
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
||
|
HttpEntity entity = response.getEntity();
|
||
|
String returnResult = EntityUtils.toString(entity, "utf-8");
|
||
|
|
||
|
FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--returnResult:"+returnResult);
|
||
|
|
||
|
return returnResult;
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--exception:"+e.getMessage());
|
||
|
}
|
||
|
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* HttpPost请求
|
||
|
* @param postMethod
|
||
|
* @return
|
||
|
*/
|
||
|
private static String HttpPost(HttpPost postMethod){
|
||
|
CloseableHttpClient httpclient = createHttpClient(null);
|
||
|
|
||
|
try {
|
||
|
HttpResponse response = httpclient.execute(postMethod);
|
||
|
|
||
|
FineLoggerFactory.getLogger().info("FRLOG:HttpPost:status:"+response.getStatusLine().getStatusCode());
|
||
|
|
||
|
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
||
|
HttpEntity entity = response.getEntity();
|
||
|
String returnResult = EntityUtils.toString(entity, "utf-8");
|
||
|
|
||
|
FineLoggerFactory.getLogger().info("FRLOG:HttpPost:returnResult:"+returnResult);
|
||
|
|
||
|
return returnResult;
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
FineLoggerFactory.getLogger().info("FRLOG:HttpPost:exception:"+e.getMessage());
|
||
|
}
|
||
|
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
public static String HttpPostXML(String url, String xmlParam){
|
||
|
FineLoggerFactory.getLogger().info("FRLOG:HttpPostXML:url:"+url);
|
||
|
|
||
|
HttpPost postMethod = new HttpPost(url);
|
||
|
|
||
|
postMethod.setHeader("Content-type", "text/html");
|
||
|
HttpEntity entity2 = null;
|
||
|
try {
|
||
|
entity2 = new StringEntity(xmlParam);
|
||
|
} catch (UnsupportedEncodingException e) {
|
||
|
FineLoggerFactory.getLogger().info("FRLOG:HttpPostXML:参数异常:"+e.getMessage());
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
postMethod.setEntity(entity2);
|
||
|
|
||
|
return HttpPost(postMethod);
|
||
|
}
|
||
|
|
||
|
public static String HttpPostJson(String url, String param,Map<String,String> header){
|
||
|
FineLoggerFactory.getLogger().info("FRLOG:HttpPostJSON:url:"+url);
|
||
|
|
||
|
HttpPost postMethod = new HttpPost(url);
|
||
|
|
||
|
postMethod.setHeader("Content-Type","application/json");
|
||
|
|
||
|
if(header != null && header.size() > 0){
|
||
|
Set<String> keySet = header.keySet();
|
||
|
|
||
|
for(String key : keySet){
|
||
|
postMethod.setHeader(key,header.get(key));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if(!Utils.isNullStr(param)){
|
||
|
HttpEntity entity2 = null;
|
||
|
try {
|
||
|
entity2 = new StringEntity(param);
|
||
|
} catch (UnsupportedEncodingException e) {
|
||
|
FineLoggerFactory.getLogger().info("FRLOG:HttpPostJSON:参数异常:"+e.getMessage());
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
postMethod.setEntity(entity2);
|
||
|
}
|
||
|
|
||
|
return HttpPost(postMethod);
|
||
|
}
|
||
|
|
||
|
public static String HttpPostWWWForm(String url, Map<String,String> header,Map<String,String> param){
|
||
|
FineLoggerFactory.getLogger().info("FRLOG:HttpWWWForm:url:"+url);
|
||
|
|
||
|
HttpPost postMethod = new HttpPost(url);
|
||
|
|
||
|
if(header != null && header.size() > 0){
|
||
|
Set<String> keySet = header.keySet();
|
||
|
|
||
|
for(String key : keySet){
|
||
|
postMethod.setHeader(key,header.get(key));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if(param != null && param.size() > 0){
|
||
|
List<NameValuePair> params = new ArrayList<NameValuePair>(param.size());
|
||
|
|
||
|
for(Map.Entry<String,String> map : param.entrySet()){
|
||
|
params.add(new BasicNameValuePair(map.getKey(), map.getValue()));
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
postMethod.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
|
||
|
} catch (UnsupportedEncodingException e) {
|
||
|
FineLoggerFactory.getLogger().info("FRLOG:HttpWWWForm:异常:"+e.getMessage());
|
||
|
return "";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return HttpPost(postMethod);
|
||
|
}
|
||
|
|
||
|
private static CloseableHttpClient createHttpClient(Cookie[] cookies){
|
||
|
|
||
|
SSLContext sslContext = null;
|
||
|
try {
|
||
|
sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
|
||
|
@Override
|
||
|
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
|
||
|
return true;
|
||
|
}
|
||
|
}).build();
|
||
|
} catch (NoSuchAlgorithmException e) {
|
||
|
FRUtils.FRLogInfo("createHttpClientException:"+e.getMessage());
|
||
|
} catch (KeyManagementException e) {
|
||
|
FRUtils.FRLogInfo("createHttpClientException:"+e.getMessage());
|
||
|
} catch (KeyStoreException e) {
|
||
|
FRUtils.FRLogInfo("createHttpClientException:"+e.getMessage());
|
||
|
}
|
||
|
|
||
|
CloseableHttpClient httpclient = null;
|
||
|
|
||
|
if(cookies != null && cookies.length > 0){
|
||
|
CookieStore cookieStore = cookieToCookieStore(cookies);
|
||
|
|
||
|
httpclient = HttpClients.custom().setSslcontext(sslContext).
|
||
|
setSSLHostnameVerifier(new NoopHostnameVerifier()).setDefaultCookieStore(cookieStore).build();
|
||
|
}
|
||
|
else{
|
||
|
httpclient = HttpClients.custom().setSslcontext(sslContext).
|
||
|
setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
|
||
|
}
|
||
|
|
||
|
return httpclient;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* cookies转cookieStore
|
||
|
* @param cookies
|
||
|
* @return
|
||
|
*/
|
||
|
public static CookieStore cookieToCookieStore(Cookie[] cookies){
|
||
|
CookieStore cookieStore = new BasicCookieStore();
|
||
|
|
||
|
if(cookies != null && cookies.length>0){
|
||
|
for(Cookie cookie : cookies){
|
||
|
BasicClientCookie cookie1 = new BasicClientCookie(cookie.getName(), cookie.getValue());
|
||
|
cookieStore.addCookie(cookie1);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return cookieStore;
|
||
|
}
|
||
|
}
|