|
@ -1,6 +1,6 @@
|
|
package com.yihu.ehr.framework.util.httpclient;
|
|
package com.yihu.ehr.framework.util.httpclient;
|
|
|
|
|
|
|
|
|
|
|
|
import com.yihu.ehr.framework.util.encode.Base64;
|
|
import org.apache.http.Consts;
|
|
import org.apache.http.Consts;
|
|
import org.apache.http.HttpEntity;
|
|
import org.apache.http.HttpEntity;
|
|
import org.apache.http.NameValuePair;
|
|
import org.apache.http.NameValuePair;
|
|
@ -10,133 +10,171 @@ import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
import org.apache.http.client.methods.*;
|
|
import org.apache.http.client.methods.*;
|
|
import org.apache.http.client.protocol.HttpClientContext;
|
|
import org.apache.http.client.protocol.HttpClientContext;
|
|
import org.apache.http.client.utils.URLEncodedUtils;
|
|
import org.apache.http.client.utils.URLEncodedUtils;
|
|
|
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
|
|
import org.apache.http.entity.ContentType;
|
|
|
|
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
|
|
import org.apache.http.entity.mime.content.FileBody;
|
|
import org.apache.http.impl.client.BasicCredentialsProvider;
|
|
import org.apache.http.impl.client.BasicCredentialsProvider;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
|
|
import org.apache.http.protocol.HTTP;
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.springframework.util.StringUtils;
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.nio.charset.Charset;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
/**
|
|
* HTTP 请求工具类
|
|
|
|
*
|
|
|
|
* @author : cwd
|
|
|
|
* @version : 1.0.0
|
|
|
|
* @date : 2016/1/14
|
|
|
|
* @see : TODO
|
|
|
|
|
|
* Created by hzp on 2016/4/14.
|
|
*/
|
|
*/
|
|
public class HttpClientUtil {
|
|
public class HttpClientUtil {
|
|
/**
|
|
|
|
* 发送 GET 请求(HTTP),不带输入数据 不加密
|
|
|
|
*
|
|
|
|
* @param url
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static String doGet(String url) throws Exception {
|
|
|
|
return doGet(url, new HashMap<String, Object>(), "", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 发送 GET 请求(HTTP),不带输入数据 不加密
|
|
|
|
*
|
|
|
|
* @param url
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static String doGet(String url, Map<String, Object> params) throws Exception {
|
|
|
|
return doGet(url, params, "", "");
|
|
|
|
|
|
/**************************** 私有方法 *****************************************/
|
|
|
|
private static CloseableHttpClient getCloseableHttpClient(SSLConnectionSocketFactory ssl) {
|
|
|
|
if(ssl == null)
|
|
|
|
{
|
|
|
|
return HttpClients.createDefault();
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
CloseableHttpClient httpClient = HttpClients.custom()
|
|
|
|
.setSSLSocketFactory(ssl)
|
|
|
|
.build();
|
|
|
|
return httpClient;
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
|
|
* 发送 GET 请求(HTTP),不带输入数据 加密
|
|
|
|
*
|
|
|
|
* @param url
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static String doGet(String url, String username, String password) throws Exception {
|
|
|
|
return doGet(url, new HashMap<String, Object>(), username, password);
|
|
|
|
|
|
private static void close(CloseableHttpClient httpClient, CloseableHttpResponse response) {
|
|
|
|
try {
|
|
|
|
if (httpClient != null) {
|
|
|
|
httpClient.close();
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (response != null) {
|
|
|
|
response.close();
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private static HttpRequestBase getRequest(String method,String url,Map<String,Object> params,Map<String,Object> header) throws Exception
|
|
|
|
{
|
|
|
|
List<BasicNameValuePair> jsonParams = new ArrayList<>();
|
|
|
|
//配置参数
|
|
|
|
if(params!=null) {
|
|
|
|
for (String key : params.keySet()) {
|
|
|
|
jsonParams.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
HttpRequestBase request;
|
|
|
|
if(method.equals("POST"))
|
|
|
|
{
|
|
|
|
request = new HttpPost(url + "?" + URLEncodedUtils.format(jsonParams, Consts.UTF_8));
|
|
|
|
}
|
|
|
|
else if(method.equals("PUT"))
|
|
|
|
{
|
|
|
|
request = new HttpPut(url + "?" + URLEncodedUtils.format(jsonParams, Consts.UTF_8));
|
|
|
|
}
|
|
|
|
else if(method.equals("DELETE"))
|
|
|
|
{
|
|
|
|
request = new HttpDelete(url + "?" + URLEncodedUtils.format(jsonParams, Consts.UTF_8));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
request = new HttpGet(url + "?" + URLEncodedUtils.format(jsonParams, Consts.UTF_8));
|
|
|
|
}
|
|
|
|
//配置头部信息
|
|
|
|
if(header!=null)
|
|
|
|
{
|
|
|
|
for (String key : header.keySet()) {
|
|
|
|
request.addHeader(key, header.get(key).toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
/****************************** 公用方法 *******************************************/
|
|
/**
|
|
/**
|
|
* httpClient的get请求方式
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* @throws Exception
|
|
|
|
|
|
* get请求
|
|
*/
|
|
*/
|
|
public static String doGet(String url, Map<String, Object> params, String username, String password)
|
|
|
|
throws Exception {
|
|
|
|
String responString = "";
|
|
|
|
CloseableHttpResponse response1 = null;
|
|
|
|
List<BasicNameValuePair> jsonParams = new ArrayList<>();
|
|
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
|
//设置请求信息
|
|
|
|
|
|
public static HttpResponse request(String method,String url,Map<String,Object> params,Map<String,Object> header,SSLConnectionSocketFactory ssl,String user,String password) {
|
|
|
|
HttpResponse re = new HttpResponse();
|
|
|
|
CloseableHttpResponse response = null;
|
|
|
|
CloseableHttpClient httpclient = getCloseableHttpClient(ssl);
|
|
|
|
|
|
|
|
//设置请求信息
|
|
try {
|
|
try {
|
|
RequestConfig requestConfig = RequestConfig.custom().
|
|
RequestConfig requestConfig = RequestConfig.custom().
|
|
setAuthenticationEnabled(true).build();
|
|
setAuthenticationEnabled(true).build();
|
|
|
|
HttpRequestBase request = getRequest(method,url,params,header);
|
|
|
|
|
|
//配置参数
|
|
|
|
for (String key : params.keySet()) {
|
|
|
|
jsonParams.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
|
|
|
|
}
|
|
|
|
|
|
|
|
HttpGet httpget = new HttpGet(url + "?" + URLEncodedUtils.format(jsonParams, Consts.UTF_8));
|
|
|
|
httpget.setConfig(requestConfig);
|
|
|
|
if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
|
|
|
|
//需要验证
|
|
|
|
|
|
request.setConfig(requestConfig);
|
|
|
|
//需要验证
|
|
|
|
if (!StringUtils.isEmpty(user) && !StringUtils.isEmpty(password)) {
|
|
HttpClientContext context = HttpClientContext.create();
|
|
HttpClientContext context = HttpClientContext.create();
|
|
//通过http的上下文设置账号密码
|
|
//通过http的上下文设置账号密码
|
|
CredentialsProvider credsProvider = new BasicCredentialsProvider();
|
|
CredentialsProvider credsProvider = new BasicCredentialsProvider();
|
|
credsProvider.setCredentials(new org.apache.http.auth.AuthScope(org.apache.http.auth.AuthScope.ANY_HOST, org.apache.http.auth.AuthScope.ANY_PORT),
|
|
|
|
new org.apache.http.auth.UsernamePasswordCredentials(username, password));
|
|
|
|
|
|
credsProvider.setCredentials(new org.apache.http.auth.AuthScope(org.apache.http.auth.AuthScope.ANY_HOST, org.apache.http.auth.AuthScope.ANY_PORT),new org.apache.http.auth.UsernamePasswordCredentials(user, password));
|
|
context.setCredentialsProvider(credsProvider);
|
|
context.setCredentialsProvider(credsProvider);
|
|
response1 = httpclient.execute(httpget, context);
|
|
|
|
|
|
response = httpclient.execute(request, context);
|
|
} else {
|
|
} else {
|
|
response1 = httpclient.execute(httpget);
|
|
|
|
|
|
response = httpclient.execute(request);
|
|
}
|
|
}
|
|
HttpEntity entity1 = response1.getEntity();
|
|
|
|
responString = EntityUtils.toString(entity1, "UTF-8");
|
|
|
|
|
|
re.setStatusCode(response.getStatusLine().getStatusCode());
|
|
|
|
re.setBody(EntityUtils.toString(response.getEntity(), "UTF-8"));
|
|
|
|
} catch (Exception e) {
|
|
|
|
re.setStatusCode(201);
|
|
|
|
re.setBody(e.getMessage());
|
|
|
|
e.printStackTrace();
|
|
} finally {
|
|
} finally {
|
|
response1.close();
|
|
|
|
httpclient.close();
|
|
|
|
|
|
close(httpclient, response);
|
|
}
|
|
}
|
|
return responString;
|
|
|
|
|
|
return re;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* httpClient的post请求方式
|
|
|
|
|
|
* 发送文件
|
|
*
|
|
*
|
|
* @param url
|
|
|
|
* @param params
|
|
|
|
* @param username
|
|
|
|
* @param password
|
|
|
|
|
|
* @param url 路径
|
|
|
|
* @param formParams 参数
|
|
* @return
|
|
* @return
|
|
* @throws Exception
|
|
|
|
*/
|
|
*/
|
|
public static String doPost(String url, Map<String, Object> params, String username, String password) throws Exception {
|
|
|
|
String responString = "";
|
|
|
|
|
|
public static HttpResponse postFile(String url,
|
|
|
|
File file, List<NameValuePair> formParams,SSLConnectionSocketFactory ssl, String username, String password) {
|
|
|
|
HttpResponse re = new HttpResponse();
|
|
CloseableHttpResponse response = null;
|
|
CloseableHttpResponse response = null;
|
|
List<BasicNameValuePair> jsonParams = new ArrayList<>();
|
|
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
|
|
|
CloseableHttpClient httpClient = getCloseableHttpClient(ssl);
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
|
try {
|
|
|
|
HttpPost httpPost = new HttpPost(url);
|
|
|
|
//设置请求信息
|
|
//设置请求信息
|
|
RequestConfig requestConfig = RequestConfig.custom().
|
|
RequestConfig requestConfig = RequestConfig.custom().
|
|
setAuthenticationEnabled(true).build();
|
|
setAuthenticationEnabled(true).build();
|
|
//设置参数
|
|
|
|
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
|
|
|
|
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
|
|
|
formparams.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));
|
|
|
|
}
|
|
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
|
|
|
|
|
|
//创建httppost请求
|
|
|
|
HttpPost httpPost = new HttpPost(url);
|
|
|
|
|
|
httpPost.setEntity(entity);
|
|
|
|
httpPost.setConfig(requestConfig);
|
|
httpPost.setConfig(requestConfig);
|
|
|
|
//新建文件对象并且设置文件
|
|
|
|
FileBody bin = new FileBody(file);
|
|
|
|
MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
|
|
|
|
reqEntity.addPart("file", bin);
|
|
|
|
//设置参数
|
|
|
|
if (formParams != null && formParams.size() > 0) {
|
|
|
|
for (NameValuePair nv : formParams) {
|
|
|
|
reqEntity.addTextBody(nv.getName(), nv.getValue(), ContentType.create("text/plain", Charset.forName(HTTP.UTF_8)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
httpPost.setEntity(reqEntity.build());
|
|
|
|
//设置验证
|
|
if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
|
|
if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
|
|
//需要验证
|
|
//需要验证
|
|
HttpClientContext context = HttpClientContext.create();
|
|
HttpClientContext context = HttpClientContext.create();
|
|
@ -145,48 +183,44 @@ public class HttpClientUtil {
|
|
credsProvider.setCredentials(new org.apache.http.auth.AuthScope(org.apache.http.auth.AuthScope.ANY_HOST, org.apache.http.auth.AuthScope.ANY_PORT),
|
|
credsProvider.setCredentials(new org.apache.http.auth.AuthScope(org.apache.http.auth.AuthScope.ANY_HOST, org.apache.http.auth.AuthScope.ANY_PORT),
|
|
new org.apache.http.auth.UsernamePasswordCredentials(username, password));
|
|
new org.apache.http.auth.UsernamePasswordCredentials(username, password));
|
|
context.setCredentialsProvider(credsProvider);
|
|
context.setCredentialsProvider(credsProvider);
|
|
response = httpclient.execute(httpPost, context);
|
|
|
|
|
|
response = httpClient.execute(httpPost, context);
|
|
} else {
|
|
} else {
|
|
response = httpclient.execute(httpPost);
|
|
|
|
|
|
response = httpClient.execute(httpPost);
|
|
}
|
|
}
|
|
HttpEntity httpEntity = response.getEntity();
|
|
|
|
//流转字符串
|
|
|
|
responString = EntityUtils.toString(httpEntity);
|
|
|
|
} catch (Exception e){
|
|
|
|
|
|
re.setStatusCode(response.getStatusLine().getStatusCode());
|
|
|
|
re.setBody(EntityUtils.toString(response.getEntity(), "UTF-8"));;
|
|
|
|
} catch (Exception e) {
|
|
|
|
re.setStatusCode(201);
|
|
|
|
re.setBody(e.getMessage());
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}finally {
|
|
|
|
response.close();
|
|
|
|
httpclient.close();
|
|
|
|
|
|
} finally {
|
|
|
|
close(httpClient, response);
|
|
}
|
|
}
|
|
return responString;
|
|
|
|
|
|
return re;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* httpClient的put请求方式
|
|
|
|
*
|
|
|
|
* @param url
|
|
|
|
* @param params
|
|
|
|
* @param username
|
|
|
|
* @param password
|
|
|
|
* @return
|
|
|
|
* @throws Exception
|
|
|
|
|
|
* 发送File
|
|
*/
|
|
*/
|
|
public static String doPut(String url, Map<String, Object> params, String username, String password) throws Exception {
|
|
|
|
String responString = "";
|
|
|
|
CloseableHttpResponse response1 = null;
|
|
|
|
|
|
public static File downLoadFileByBase64(String filePath, Map<String, Object> params, String url, String username, String password) {
|
|
|
|
File file = null;
|
|
|
|
CloseableHttpResponse response = null;
|
|
List<BasicNameValuePair> jsonParams = new ArrayList<>();
|
|
List<BasicNameValuePair> jsonParams = new ArrayList<>();
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
|
|
|
CloseableHttpClient httpclient = getCloseableHttpClient(null);
|
|
|
|
|
|
try {
|
|
try {
|
|
|
|
HttpPost httpPost = new HttpPost(url);
|
|
//设置请求信息
|
|
//设置请求信息
|
|
RequestConfig requestConfig = RequestConfig.custom().
|
|
RequestConfig requestConfig = RequestConfig.custom().
|
|
setAuthenticationEnabled(true).build();
|
|
setAuthenticationEnabled(true).build();
|
|
|
|
|
|
//配置参数
|
|
|
|
for (String key : params.keySet()) {
|
|
|
|
jsonParams.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
|
|
|
|
|
|
//设置参数
|
|
|
|
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
|
|
|
|
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
|
|
|
formparams.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));
|
|
}
|
|
}
|
|
HttpPut httpPut = new HttpPut(url + "?" + URLEncodedUtils.format(jsonParams, Consts.UTF_8));
|
|
|
|
httpPut.setConfig(requestConfig);
|
|
|
|
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
|
|
|
|
httpPost.setEntity(entity);
|
|
|
|
httpPost.setConfig(requestConfig);
|
|
if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
|
|
if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
|
|
//需要验证
|
|
//需要验证
|
|
HttpClientContext context = HttpClientContext.create();
|
|
HttpClientContext context = HttpClientContext.create();
|
|
@ -195,45 +229,51 @@ public class HttpClientUtil {
|
|
credsProvider.setCredentials(new org.apache.http.auth.AuthScope(org.apache.http.auth.AuthScope.ANY_HOST, org.apache.http.auth.AuthScope.ANY_PORT),
|
|
credsProvider.setCredentials(new org.apache.http.auth.AuthScope(org.apache.http.auth.AuthScope.ANY_HOST, org.apache.http.auth.AuthScope.ANY_PORT),
|
|
new org.apache.http.auth.UsernamePasswordCredentials(username, password));
|
|
new org.apache.http.auth.UsernamePasswordCredentials(username, password));
|
|
context.setCredentialsProvider(credsProvider);
|
|
context.setCredentialsProvider(credsProvider);
|
|
response1 = httpclient.execute(httpPut, context);
|
|
|
|
|
|
response = httpclient.execute(httpPost, context);
|
|
} else {
|
|
} else {
|
|
response1 = httpclient.execute(httpPut);
|
|
|
|
|
|
response = httpclient.execute(httpPost);
|
|
|
|
}
|
|
|
|
HttpEntity httpEntity = response.getEntity();
|
|
|
|
String responString = EntityUtils.toString(httpEntity, "UTF-8");
|
|
|
|
file = new File(filePath);
|
|
|
|
file.getParentFile().mkdirs();
|
|
|
|
InputStream i = new ByteArrayInputStream(Base64.decode(responString));
|
|
|
|
FileOutputStream fileout = new FileOutputStream(file);
|
|
|
|
/**
|
|
|
|
* 根据实际运行效果 设置缓冲区大小
|
|
|
|
*/
|
|
|
|
byte[] buffer = new byte[1024];
|
|
|
|
int ch = 0;
|
|
|
|
while ((ch = i.read(buffer)) != -1) {
|
|
|
|
fileout.write(buffer, 0, ch);
|
|
}
|
|
}
|
|
HttpEntity entity1 = response1.getEntity();
|
|
|
|
responString = EntityUtils.toString(entity1, "UTF-8");
|
|
|
|
|
|
i.close();
|
|
|
|
fileout.flush();
|
|
|
|
fileout.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
|
} finally {
|
|
} finally {
|
|
response1.close();
|
|
|
|
httpclient.close();
|
|
|
|
|
|
close(httpclient, response);
|
|
}
|
|
}
|
|
return responString;
|
|
|
|
|
|
return file;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* httpClient的delete请求方式
|
|
|
|
*
|
|
|
|
* @param url
|
|
|
|
* @param params
|
|
|
|
* @param username
|
|
|
|
* @param password
|
|
|
|
* @return
|
|
|
|
* @throws Exception
|
|
|
|
|
|
* 下载文件
|
|
*/
|
|
*/
|
|
public static String doDelete(String url, Map<String, Object> params, String username, String password) throws Exception {
|
|
|
|
String responString = "";
|
|
|
|
CloseableHttpResponse response1 = null;
|
|
|
|
|
|
public static File downLoadFile(String filePath, String url, String username, String password) {
|
|
|
|
File file = null;
|
|
|
|
CloseableHttpResponse response = null;
|
|
List<BasicNameValuePair> jsonParams = new ArrayList<>();
|
|
List<BasicNameValuePair> jsonParams = new ArrayList<>();
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
|
|
|
CloseableHttpClient httpclient = getCloseableHttpClient(null);
|
|
|
|
|
|
try {
|
|
try {
|
|
|
|
HttpGet httpGet = new HttpGet(url);
|
|
//设置请求信息
|
|
//设置请求信息
|
|
RequestConfig requestConfig = RequestConfig.custom().
|
|
RequestConfig requestConfig = RequestConfig.custom().
|
|
setAuthenticationEnabled(true).build();
|
|
setAuthenticationEnabled(true).build();
|
|
|
|
|
|
//配置参数
|
|
|
|
for (String key : params.keySet()) {
|
|
|
|
jsonParams.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
|
|
|
|
}
|
|
|
|
HttpDelete httpget = new HttpDelete(url + "?" + URLEncodedUtils.format(jsonParams, Consts.UTF_8));
|
|
|
|
httpget.setConfig(requestConfig);
|
|
|
|
if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
|
|
if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
|
|
//需要验证
|
|
//需要验证
|
|
HttpClientContext context = HttpClientContext.create();
|
|
HttpClientContext context = HttpClientContext.create();
|
|
@ -242,34 +282,35 @@ public class HttpClientUtil {
|
|
credsProvider.setCredentials(new org.apache.http.auth.AuthScope(org.apache.http.auth.AuthScope.ANY_HOST, org.apache.http.auth.AuthScope.ANY_PORT),
|
|
credsProvider.setCredentials(new org.apache.http.auth.AuthScope(org.apache.http.auth.AuthScope.ANY_HOST, org.apache.http.auth.AuthScope.ANY_PORT),
|
|
new org.apache.http.auth.UsernamePasswordCredentials(username, password));
|
|
new org.apache.http.auth.UsernamePasswordCredentials(username, password));
|
|
context.setCredentialsProvider(credsProvider);
|
|
context.setCredentialsProvider(credsProvider);
|
|
response1 = httpclient.execute(httpget, context);
|
|
|
|
|
|
response = httpclient.execute(httpGet, context);
|
|
} else {
|
|
} else {
|
|
response1 = httpclient.execute(httpget);
|
|
|
|
|
|
response = httpclient.execute(httpGet);
|
|
}
|
|
}
|
|
HttpEntity entity1 = response1.getEntity();
|
|
|
|
responString = EntityUtils.toString(entity1, "UTF-8");
|
|
|
|
|
|
HttpEntity httpEntity = response.getEntity();
|
|
|
|
InputStream is = httpEntity.getContent();
|
|
|
|
file = new File(filePath);
|
|
|
|
file.getParentFile().mkdirs();
|
|
|
|
FileOutputStream fileout = new FileOutputStream(file);
|
|
|
|
/**
|
|
|
|
* 根据实际运行效果 设置缓冲区大小
|
|
|
|
*/
|
|
|
|
byte[] buffer = new byte[1024];
|
|
|
|
int ch = 0;
|
|
|
|
while ((ch = is.read(buffer)) != -1) {
|
|
|
|
fileout.write(buffer, 0, ch);
|
|
|
|
}
|
|
|
|
is.close();
|
|
|
|
fileout.flush();
|
|
|
|
fileout.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
|
} finally {
|
|
} finally {
|
|
response1.close();
|
|
|
|
httpclient.close();
|
|
|
|
|
|
close(httpclient, response);
|
|
}
|
|
}
|
|
return responString;
|
|
|
|
|
|
return file;
|
|
}
|
|
}
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
Map<String, Object> params = new HashMap<String, Object>();
|
|
|
|
// params.put("standard", "{\n" +
|
|
|
|
// " \"id\": 4,\n" +
|
|
|
|
// " \"name\": \"电子健康档案标准bbb\",\n" +
|
|
|
|
// " \"code\": \"YY20160114\",\n" +
|
|
|
|
// " \"publisher\": \"健康之路2\",\n" +
|
|
|
|
// " \"publisherOrgCode\": \"jkzl\",\n" +
|
|
|
|
// " \"summary\": \"string\",\n" +
|
|
|
|
// " \"refStandard\": \"WS2012\",\n" +
|
|
|
|
// " \"refStandardVersion\": \"V1.0\",\n" +
|
|
|
|
// " \"versionStatus\": 0\n" +
|
|
|
|
// "}");
|
|
|
|
params.put("versionId", "5694696eb80d");
|
|
|
|
params.put("publisher", "lfq");
|
|
|
|
System.out.println(HttpClientUtil.doPost("http://192.168.131.17:6020/api/v1.0/standard_center/version/publish", params, "user", "standard"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|