import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class HttpUtil {
private static ThreadLocal<CloseableHttpClient> httpClientContainer = new ThreadLocal<CloseableHttpClient>();
private static final String CHARSET = "UTF-8";
public static final String CONTENT_TYPE_XML = "application/xml";
public static final String CONTENT_TYPE_JSON = "application/json";
public static final int CONNECT_TIMEOUT_NORMAL = 30000;
public static final int SOCKET_TIMEOUT_NORMAL = 30000;
public static final int CONNECT_TIMEOUT_MAX = 120000;
public static final int SOCKET_TIMEOUT_MAX = 120000;
public static Logger logger = LoggerFactory.getLogger(HttpUtil.class);
public static String postSoapRequest(String soapUrl,String xmlDate) {
logger.info("url:" + soapUrl);
logger.info("params:" + xmlDate);
if (StringUtils.isBlank(soapUrl)) {
return null;
}
CloseableHttpClient httpClient = null;
httpClient = getHttpClientNormal();
CloseableHttpResponse response = null;
try {
HttpPost httpPost = new HttpPost(soapUrl);
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
StringEntity s = new StringEntity(xmlDate);
s.setContentEncoding(CHARSET);
s.setContentType(CONTENT_TYPE_XML);
httpPost.setEntity(s);
// 如果有请求头参数,添加进header中
// if (null != header && !header.isEmpty()) {
// for (String key : header.keySet()) {
// httpPost.addHeader(key, header.get(key));
// }
// }
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
httpPost.abort();
throw new RuntimeException("HttpClient,error status code :" + statusCode);
}
HttpEntity entity = response.getEntity();
String result = null;
if (entity != null) {
result = EntityUtils.toString(entity, CHARSET);
}
logger.info("result:" + result);
EntityUtils.consume(entity);
return result;
} catch (Exception e) {
logger.error("发送http请求失败,错误信息是{}",e);
throw new RuntimeException(e);
} finally {
closeHttpClient(response);
}
}
}
HttpClient 发送soap 请求方法
猜你喜欢
转载自blog.csdn.net/u012149894/article/details/90478921
今日推荐
周排行