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.
408 lines
16 KiB
408 lines
16 KiB
package com.kingdee.eas.custom.risheng.util; |
|
|
|
import java.util.ArrayList; |
|
import java.util.HashMap; |
|
import java.util.List; |
|
import java.util.Map; |
|
import java.util.Map.Entry; |
|
|
|
import org.apache.commons.httpclient.HttpStatus; |
|
import org.apache.commons.logging.Log; |
|
import org.apache.commons.logging.LogFactory; |
|
import org.apache.http.HttpEntity; |
|
import org.apache.http.client.config.RequestConfig; |
|
import org.apache.http.client.methods.CloseableHttpResponse; |
|
import org.apache.http.client.methods.HttpGet; |
|
import org.apache.http.client.methods.HttpPost; |
|
import org.apache.http.entity.StringEntity; |
|
import org.apache.http.impl.client.CloseableHttpClient; |
|
import org.apache.http.impl.client.HttpClients; |
|
import org.apache.http.protocol.BasicHttpContext; |
|
import org.apache.http.util.EntityUtils; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import java.io.IOException; |
|
|
|
import org.apache.http.NameValuePair; |
|
import org.apache.http.client.entity.UrlEncodedFormEntity; |
|
import org.apache.http.message.BasicNameValuePair; |
|
|
|
public class HttpRequestSecUtil { |
|
|
|
private static final Log log = LogFactory.getLog(HttpRequestSecUtil.class); |
|
|
|
public static String sendPost(String url, String content) throws Exception { |
|
return sendPost(url,content,"json"); |
|
|
|
} |
|
public static String sendPost(String url, String content, Map<String, String> headMap) throws Exception { |
|
return sendPostAndHeader(url,content,"json",headMap); |
|
} |
|
|
|
public static String sendPost(String url, String content, String contentType, Map<String, String> headMap) throws Exception { |
|
return sendPostAndHeader(url,content,contentType,headMap); |
|
} |
|
|
|
public static String sendPostJson(String url, String content,String contentType) throws Exception { |
|
log.info(url.toString()); |
|
CloseableHttpClient httpClient = null; |
|
HttpPost httpPost = null; |
|
CloseableHttpResponse httpResponse = null; |
|
try { |
|
httpClient = HttpClients.createDefault(); |
|
int CONNECTION_TIMEOUT = 10 * 1000; |
|
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(CONNECTION_TIMEOUT).setConnectTimeout(CONNECTION_TIMEOUT).setSocketTimeout(CONNECTION_TIMEOUT).build(); |
|
httpPost = new HttpPost(url); |
|
httpPost.setConfig(requestConfig); |
|
if("json".equals(contentType)) |
|
{ |
|
httpPost.addHeader("Content-Type", "application/json"); |
|
httpPost.addHeader("Accept", "application/json"); |
|
} |
|
else |
|
{ |
|
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded"); |
|
} |
|
StringEntity requestEntity = new StringEntity(content, "UTF-8"); |
|
httpPost.setEntity(requestEntity); |
|
httpResponse = httpClient.execute(httpPost, new BasicHttpContext()); |
|
HttpEntity entity = httpResponse.getEntity(); |
|
if(entity != null) { |
|
String resultStr = EntityUtils.toString(entity, "UTF-8"); |
|
return resultStr; |
|
} |
|
} catch(Exception e) { |
|
log.error("", e); |
|
throw new Exception(e); |
|
} finally { |
|
if(httpResponse != null) { |
|
try { |
|
httpResponse.close(); |
|
} catch(Exception e) { |
|
throw new Exception(e); |
|
} |
|
} |
|
if(httpPost != null) { |
|
try { |
|
httpPost.releaseConnection(); |
|
} catch(Exception e) { |
|
log.error("", e); |
|
throw new Exception(e); |
|
} |
|
} |
|
if(httpClient != null) { |
|
try { |
|
httpClient.close(); |
|
} catch(Exception e) { |
|
log.error("", e); |
|
throw new Exception(e); |
|
} |
|
} |
|
} |
|
return null; |
|
} |
|
|
|
public static String sendPost(String url, String content,String contentType) throws Exception { |
|
log.info(url); |
|
CloseableHttpClient httpClient = null; |
|
HttpPost httpPost = null; |
|
CloseableHttpResponse httpResponse = null; |
|
String resulrCode = ""; |
|
try { |
|
httpClient = HttpClients.createDefault(); |
|
int CONNECTION_TIMEOUT = 10 * 1000; |
|
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(CONNECTION_TIMEOUT).setConnectTimeout(CONNECTION_TIMEOUT).setSocketTimeout(CONNECTION_TIMEOUT).build(); |
|
httpPost = new HttpPost(url); |
|
httpPost.setConfig(requestConfig); |
|
if("json".equals(contentType)) |
|
{ |
|
httpPost.addHeader("Content-Type", "application/json"); |
|
httpPost.addHeader("Accept", "application/json"); |
|
//httpPost.addHeader("Authorization", "Bearer "+SrmUtil.getToken()); |
|
} |
|
else |
|
{ |
|
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded"); |
|
} |
|
StringEntity requestEntity = new StringEntity(content, "UTF-8"); |
|
httpPost.setEntity(requestEntity); |
|
httpResponse = httpClient.execute(httpPost, new BasicHttpContext()); |
|
int code = httpResponse.getStatusLine().getStatusCode(); |
|
resulrCode = String.valueOf(code); |
|
if(code != HttpStatus.SC_OK){ |
|
return resulrCode; |
|
|
|
} |
|
HttpEntity entity = httpResponse.getEntity(); |
|
if(entity != null) { |
|
String resultStr = EntityUtils.toString(entity, "UTF-8"); |
|
return resultStr; |
|
} |
|
} catch(Exception e) { |
|
log.error("", e); |
|
throw new Exception(e); |
|
} finally { |
|
if(httpResponse != null) { |
|
try { |
|
httpResponse.close(); |
|
} catch(Exception e) { |
|
log.error("", e); |
|
throw new Exception(e); |
|
} |
|
} |
|
if(httpPost != null) { |
|
try { |
|
httpPost.releaseConnection(); |
|
} catch(Exception e) { |
|
log.error("", e); |
|
throw new Exception(e); |
|
} |
|
} |
|
if(httpClient != null) { |
|
try { |
|
httpClient.close(); |
|
} catch(Exception e) { |
|
log.error("", e); |
|
throw new Exception(e); |
|
} |
|
} |
|
} |
|
return resulrCode; |
|
} |
|
|
|
|
|
|
|
public static String sendPostAndHeader(String url, String content,String contentType, Map<String, String> headMap) throws Exception { |
|
log.info(url); |
|
CloseableHttpClient httpClient = null; |
|
HttpPost httpPost = null; |
|
CloseableHttpResponse httpResponse = null; |
|
String resulrCode = ""; |
|
try { |
|
httpClient = HttpClients.createDefault(); |
|
int CONNECTION_TIMEOUT = 100 * 1000; |
|
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(CONNECTION_TIMEOUT).setConnectTimeout(CONNECTION_TIMEOUT).setSocketTimeout(CONNECTION_TIMEOUT).build(); |
|
httpPost = new HttpPost(url); |
|
httpPost.setConfig(requestConfig); |
|
if("json".equals(contentType)) |
|
{ |
|
httpPost.addHeader("Content-Type", "application/json"); |
|
httpPost.addHeader("Accept", "application/json"); |
|
//httpPost.addHeader("Authorization", "Bearer "+SrmUtil.getToken()); |
|
StringEntity requestEntity = new StringEntity(content, "UTF-8"); |
|
httpPost.setEntity(requestEntity); |
|
|
|
System.out.println("json----post+headMap.toString()"); |
|
} |
|
else |
|
{ |
|
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded"); |
|
List<NameValuePair> nvps = new ArrayList<NameValuePair>(); |
|
JSONObject object = JSONObject.parseObject(content); |
|
for (Entry<String, Object> entry : object.entrySet()) { |
|
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString())); |
|
} |
|
|
|
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8")); |
|
System.out.println(contentType+"----post+headMap.toString()"); |
|
} |
|
// |
|
if(headMap!=null){ |
|
for (String sourceKey : headMap.keySet()) { |
|
httpPost.addHeader(sourceKey, headMap.get(sourceKey)); |
|
} |
|
} |
|
|
|
// |
|
httpResponse = httpClient.execute(httpPost, new BasicHttpContext()); |
|
int code = httpResponse.getStatusLine().getStatusCode(); |
|
resulrCode = String.valueOf(code); |
|
if(code != HttpStatus.SC_OK){ |
|
return resulrCode; |
|
|
|
} |
|
HttpEntity entity = httpResponse.getEntity(); |
|
if(entity != null) { |
|
String resultStr = EntityUtils.toString(entity, "UTF-8"); |
|
return resultStr; |
|
} |
|
} catch(Exception e) { |
|
log.error("", e); |
|
throw new Exception(e); |
|
} finally { |
|
if(httpResponse != null) { |
|
try { |
|
httpResponse.close(); |
|
} catch(Exception e) { |
|
log.error("", e); |
|
throw new Exception(e); |
|
} |
|
} |
|
if(httpPost != null) { |
|
try { |
|
httpPost.releaseConnection(); |
|
} catch(Exception e) { |
|
log.error("", e); |
|
throw new Exception(e); |
|
} |
|
} |
|
if(httpClient != null) { |
|
try { |
|
httpClient.close(); |
|
} catch(Exception e) { |
|
log.error("", e); |
|
throw new Exception(e); |
|
} |
|
} |
|
} |
|
return resulrCode; |
|
} |
|
|
|
|
|
public static String sendGet(String url) { |
|
CloseableHttpClient httpClient = null; |
|
HttpGet httpGet = null; |
|
CloseableHttpResponse httpResponse = null; |
|
try { |
|
httpClient = HttpClients.createDefault(); |
|
int CONNECTION_TIMEOUT = 10 * 1000; |
|
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(CONNECTION_TIMEOUT).setConnectTimeout(CONNECTION_TIMEOUT).setSocketTimeout(CONNECTION_TIMEOUT).build(); |
|
httpGet = new HttpGet(url); |
|
httpGet.setConfig(requestConfig); |
|
httpGet.addHeader("Content-Type", "application/json"); |
|
httpGet.addHeader("Accept", "application/json"); |
|
httpResponse = httpClient.execute(httpGet, new BasicHttpContext()); |
|
HttpEntity entity = httpResponse.getEntity(); |
|
if(entity != null) { |
|
String resultStr = EntityUtils.toString(entity, "UTF-8"); |
|
//JSONObject result = JSONObject.parseObject(resultStr); |
|
return resultStr; |
|
} |
|
} catch(Exception e) { |
|
// log.error("", e); |
|
} finally { |
|
if(httpResponse != null) { |
|
try { |
|
httpResponse.close(); |
|
} catch(Exception e) { |
|
} |
|
} |
|
if(httpGet != null) { |
|
try { |
|
httpGet.releaseConnection(); |
|
} catch(Exception e) { |
|
} |
|
} |
|
if(httpClient != null) { |
|
try { |
|
httpClient.close(); |
|
} catch(Exception e) { |
|
} |
|
} |
|
} |
|
return null; |
|
} |
|
// public static String sendPost(String url, String content,String contentType) throws Exception { |
|
// log.info(url); |
|
// CloseableHttpClient httpClient = null; |
|
// HttpPost httpPost = null; |
|
// CloseableHttpResponse httpResponse = null; |
|
// String resulrCode = ""; |
|
// try { |
|
// httpClient = HttpClients.createDefault(); |
|
// int CONNECTION_TIMEOUT = 10 * 1000; |
|
// RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(CONNECTION_TIMEOUT).setConnectTimeout(CONNECTION_TIMEOUT).setSocketTimeout(CONNECTION_TIMEOUT).build(); |
|
// httpPost = new HttpPost(url); |
|
// httpPost.setConfig(requestConfig); |
|
// if("json".equals(contentType)) |
|
// { |
|
// httpPost.addHeader("Content-Type", "application/json"); |
|
// httpPost.addHeader("Accept", "application/json"); |
|
// //httpPost.addHeader("Authorization", "Bearer "+SrmUtil.getToken()); |
|
// } |
|
// else |
|
// { |
|
// httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded"); |
|
// } |
|
// StringEntity requestEntity = new StringEntity(content, "UTF-8"); |
|
// httpPost.setEntity(requestEntity); |
|
// httpResponse = httpClient.execute(httpPost, new BasicHttpContext()); |
|
// int code = httpResponse.getStatusLine().getStatusCode(); |
|
// resulrCode = String.valueOf(code); |
|
// if(code != HttpStatus.SC_OK){ |
|
// return resulrCode; |
|
// |
|
// } |
|
// HttpEntity entity = httpResponse.getEntity(); |
|
// if(entity != null) { |
|
// String resultStr = EntityUtils.toString(entity, "UTF-8"); |
|
// return resultStr; |
|
// } |
|
// } catch(Exception e) { |
|
// log.error("", e); |
|
// throw new Exception(e); |
|
// } finally { |
|
// if(httpResponse != null) { |
|
// try { |
|
// httpResponse.close(); |
|
// } catch(Exception e) { |
|
// log.error("", e); |
|
// throw new Exception(e); |
|
// } |
|
// } |
|
// if(httpPost != null) { |
|
// try { |
|
// httpPost.releaseConnection(); |
|
// } catch(Exception e) { |
|
// log.error("", e); |
|
// throw new Exception(e); |
|
// } |
|
// } |
|
// if(httpClient != null) { |
|
// try { |
|
// httpClient.close(); |
|
// } catch(Exception e) { |
|
// log.error("", e); |
|
// throw new Exception(e); |
|
// } |
|
// } |
|
// } |
|
// return resulrCode; |
|
// } |
|
|
|
public static String doPost(String url, Map<String, String> param) { |
|
// |
|
CloseableHttpClient httpClient = HttpClients.createDefault(); |
|
CloseableHttpResponse response = null; |
|
String resultString = ""; |
|
try { |
|
// |
|
HttpPost httpPost = new HttpPost(url); |
|
// |
|
if (param != null) { |
|
List<NameValuePair> paramList = new ArrayList<NameValuePair>(); |
|
for (String key : param.keySet()) { |
|
paramList.add(new BasicNameValuePair(key, param.get(key))); |
|
} |
|
// |
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"utf-8"); |
|
httpPost.setEntity(entity); |
|
} |
|
// |
|
response = httpClient.execute(httpPost); |
|
resultString = EntityUtils.toString(response.getEntity(), "utf-8"); |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} finally { |
|
try { |
|
response.close(); |
|
} catch (IOException e) { |
|
// TODO Auto-generated catch block |
|
e.printStackTrace(); |
|
} |
|
} |
|
return resultString; |
|
} |
|
|
|
}
|
|
|