|
@ -1,21 +1,38 @@
|
|
|
package com.yihu.ehr.framework.util.httpclient;
|
|
|
|
|
|
import com.yihu.ehr.framework.util.operator.StringUtil;
|
|
|
import org.apache.http.Header;
|
|
|
import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.NameValuePair;
|
|
|
import org.apache.http.client.CredentialsProvider;
|
|
|
import org.apache.http.client.config.RequestConfig;
|
|
|
import org.apache.http.client.entity.GzipDecompressingEntity;
|
|
|
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.client.methods.HttpRequestBase;
|
|
|
import org.apache.http.client.protocol.HttpClientContext;
|
|
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
|
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
|
|
|
import org.apache.http.entity.ContentType;
|
|
|
import org.apache.http.impl.client.BasicCredentialsProvider;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
import org.apache.http.ssl.SSLContexts;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
import org.springframework.core.io.ClassPathResource;
|
|
|
import org.springframework.core.io.Resource;
|
|
|
import org.springframework.core.io.support.EncodedResource;
|
|
|
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
import java.io.File;
|
|
|
import java.io.*;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Properties;
|
|
|
import java.util.zip.GZIPInputStream;
|
|
|
|
|
|
public class HttpHelper {
|
|
|
private static String defaultPropertiesPath = "config/http.properties";
|
|
@ -45,7 +62,8 @@ public class HttpHelper {
|
|
|
if (StringUtil.isEmpty(sslKeystore)) {
|
|
|
String home = System.getProperty("catalina.home").replace('\\','/');
|
|
|
String homeUrl = home.substring(0,home.lastIndexOf('/')+1);
|
|
|
sslKeystore = homeUrl + "tomcat.keystore";
|
|
|
// sslKeystore = homeUrl + "tomcat.keystore";
|
|
|
sslKeystore ="E://tomcat.keystore";
|
|
|
}
|
|
|
if(sslKeystore!=null && sslKeystore.length()>0 && sslPassword!=null &&sslPassword.length()>0)
|
|
|
{
|
|
@ -270,4 +288,102 @@ public class HttpHelper {
|
|
|
return HttpClientUtil.request("GET",httpGateway,params,null,null,null,null);
|
|
|
}
|
|
|
|
|
|
|
|
|
public static String getWebPage(String url){
|
|
|
String returnString="";
|
|
|
HttpResponse re = new HttpResponse();
|
|
|
CloseableHttpResponse response = null;
|
|
|
CloseableHttpClient httpclient = HttpClients.custom()
|
|
|
.setSSLSocketFactory(defaultSSL)
|
|
|
.build();
|
|
|
|
|
|
//设置请求信息
|
|
|
try {
|
|
|
HttpGet httpGet = new HttpGet(url);
|
|
|
|
|
|
response = httpclient.execute(httpGet);
|
|
|
String line;
|
|
|
// 读取输入流的数据,并显示
|
|
|
HttpEntity httpEntity= response.getEntity();
|
|
|
Header header = response.getEntity().getContentEncoding();
|
|
|
if(httpEntity.getContentLength() < 2147483647L){ //EntityUtils无法处理ContentLength超过2147483647L的Entity
|
|
|
if(header != null && "gzip".equals(header.getValue())){
|
|
|
returnString = EntityUtils.toString(new GzipDecompressingEntity(httpEntity));
|
|
|
} else {
|
|
|
returnString = EntityUtils.toString(httpEntity);
|
|
|
}
|
|
|
} else {
|
|
|
InputStream in = httpEntity.getContent();
|
|
|
if(header != null && "gzip".equals(header.getValue())){
|
|
|
returnString = unZip(in, ContentType.getOrDefault(httpEntity).getCharset().toString());
|
|
|
} else {
|
|
|
returnString = readInStreamToString(in, ContentType.getOrDefault(httpEntity).getCharset().toString());
|
|
|
}
|
|
|
if(in != null){
|
|
|
in.close();
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
re.setStatusCode(201);
|
|
|
re.setBody(e.getMessage());
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
try {
|
|
|
httpclient.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
System.out.println(returnString);
|
|
|
return returnString;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 解压服务器返回的gzip流
|
|
|
* @param in 抓取返回的InputStream流
|
|
|
* @param charSet 页面内容编码
|
|
|
* @return 页面内容的String格式
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
private static String unZip(InputStream in, String charSet) throws IOException {
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
GZIPInputStream gis = null;
|
|
|
try {
|
|
|
gis = new GZIPInputStream(in);
|
|
|
byte[] _byte = new byte[1024];
|
|
|
int len = 0;
|
|
|
while ((len = gis.read(_byte)) != -1) {
|
|
|
baos.write(_byte, 0, len);
|
|
|
}
|
|
|
String unzipString = new String(baos.toByteArray(), charSet);
|
|
|
return unzipString;
|
|
|
} finally {
|
|
|
if (gis != null) {
|
|
|
gis.close();
|
|
|
}
|
|
|
if(baos != null){
|
|
|
baos.close();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 读取InputStream流
|
|
|
* @param in InputStream流
|
|
|
* @return 从流中读取的String
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
private static String readInStreamToString(InputStream in, String charSet) throws IOException {
|
|
|
StringBuilder str = new StringBuilder();
|
|
|
String line;
|
|
|
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in, charSet));
|
|
|
while((line = bufferedReader.readLine()) != null){
|
|
|
str.append(line);
|
|
|
str.append("\n");
|
|
|
}
|
|
|
if(bufferedReader != null) {
|
|
|
bufferedReader.close();
|
|
|
}
|
|
|
return str.toString();
|
|
|
}
|
|
|
}
|