Commit 1eedd960 authored by 张永's avatar 张永

1

parent 2c952ff3
package com.egolm.common;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Proxy;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.egolm.common.exception.HttpRequestException;
......@@ -18,7 +29,77 @@ import com.egolm.common.exception.HttpRequestException;
*
*/
public class HttpsUtil {
private static RequestConfig requestConfig;
static {
RequestConfig.Builder configBuilder = RequestConfig.custom();
// 设置连接超时
configBuilder.setConnectTimeout(10*1000);
// 设置读取超时
configBuilder.setSocketTimeout(10*10000);
requestConfig = configBuilder.build();
}
/**
* 发送无参数的GET请求
*/
public static String doGet(String url) {
return doGet(url,null);
}
/**
* 发送有参数的GET请求,参数为MAP key-value格式
*/
public static String doGet(String url, Map<String, String> params) {
List<String> paramList=new ArrayList<String>();
if(params!=null){
for (String key : params.keySet()) {
paramList.add(key+"="+params.get(key));
}
if(paramList.size()>0){
if(url.indexOf("?")==-1){
url=url+"?"+StringUtil.join("&",paramList);
}else{
url=url+"&"+StringUtil.join("&",paramList);
}
}
}
System.out.println("doGet--"+url);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url.replaceAll(" ", "%20"));
httpGet.setConfig(requestConfig);
CloseableHttpResponse response = null;
HttpEntity entity = null;
try {
response = httpclient.execute(httpGet);
entity = response.getEntity();
String result = EntityUtils.toString(entity, "UTF-8");
return result;
} catch (Exception e) {
throw new HttpRequestException(e);
} finally {
try {
if(entity!=null){
EntityUtils.consume(entity);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(response!=null){
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String get(String requestUrl, Map<String, Object> parameters, Map<String, String> header, Proxy proxy, SSLSocketFactory sslSocketFactory) throws HttpRequestException {
HttpsURLConnection connection = null;
try {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment