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.
260 lines
8.7 KiB
260 lines
8.7 KiB
package com.eco.plugin.xx.gfjjwhite.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.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 httpGet(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); |
|
|
|
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); |
|
int status =response.getStatusLine().getStatusCode(); |
|
HttpEntity entity = response.getEntity(); |
|
String returnResult = EntityUtils.toString(entity, "utf-8"); |
|
|
|
FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--status:"+status+";returnResult:"+returnResult); |
|
|
|
httpclient.close(); |
|
|
|
if (status == HttpStatus.SC_OK) { |
|
return returnResult; |
|
} |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--exception:"+e.getMessage()); |
|
} |
|
|
|
try { |
|
httpclient.close(); |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().info("FRLOG:http关闭异常:"+e.getMessage()); |
|
} |
|
|
|
return ""; |
|
} |
|
|
|
/** |
|
* HttpPost请求 |
|
* @param postMethod |
|
* @return |
|
*/ |
|
private static String HttpPost(HttpPost postMethod){ |
|
CloseableHttpClient httpclient = createHttpClient(null); |
|
|
|
try { |
|
HttpResponse response = httpclient.execute(postMethod); |
|
int status = response.getStatusLine().getStatusCode(); |
|
HttpEntity entity = response.getEntity(); |
|
String returnResult = EntityUtils.toString(entity, "utf-8"); |
|
FineLoggerFactory.getLogger().info("FRLOG:HttpPost:status:"+status+";returnResult:"+returnResult); |
|
httpclient.close(); |
|
|
|
if (status == HttpStatus.SC_OK) { |
|
return returnResult; |
|
} |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().info("FRLOG:HttpPost:exception:"+e.getMessage()); |
|
} |
|
|
|
try { |
|
httpclient.close(); |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().info("FRLOG:http关闭异常:"+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 entity = null; |
|
try { |
|
entity = new StringEntity(xmlParam); |
|
} catch (UnsupportedEncodingException e) { |
|
FineLoggerFactory.getLogger().info("FRLOG:HttpPostXML:参数异常:"+e.getMessage()); |
|
return ""; |
|
} |
|
|
|
postMethod.setEntity(entity); |
|
|
|
return HttpPost(postMethod); |
|
} |
|
|
|
public static String HttpPostText(String url, String xmlParam){ |
|
FineLoggerFactory.getLogger().info("FRLOG:HttpPostText:url:"+url); |
|
|
|
HttpPost postMethod = new HttpPost(url); |
|
|
|
postMethod.setHeader("Content-type", "text/plain"); |
|
HttpEntity entity = null; |
|
try { |
|
entity = new StringEntity(xmlParam); |
|
} catch (UnsupportedEncodingException e) { |
|
FineLoggerFactory.getLogger().info("FRLOG:HttpPostText:参数异常:"+e.getMessage()); |
|
return ""; |
|
} |
|
|
|
postMethod.setEntity(entity); |
|
|
|
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 entity = null; |
|
try { |
|
entity = new StringEntity(param); |
|
} catch (UnsupportedEncodingException e) { |
|
FineLoggerFactory.getLogger().info("FRLOG:HttpPostJSON:参数异常:"+e.getMessage()); |
|
return ""; |
|
} |
|
|
|
postMethod.setEntity(entity); |
|
} |
|
|
|
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 (Exception e) { |
|
FRUtils.FRLogInfo("exception:"+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; |
|
} |
|
}
|
|
|