//http工具类:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.xml.DomUtils;
import org.springframework.util.xml.SimpleSaxErrorHandler;
import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public abstract class HttpUtil {
private static final String USER_AGENT = "HttpClient/4.5 PaasAPIUtil";
private static final Log logger = LogFactory.getLog(HttpUtil.class);
public static enum DataType {
JSON, XML, FORM, FILE
}
public static class HttpFormFile {
private String filePath;
private String fileName;
public HttpFormFile() {
}
public HttpFormFile(String path, String name) {
this.filePath = path;
this.fileName = name;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
@SuppressWarnings({ "deprecation", "unchecked" })
private static HttpEntity bulidHttpEntiry(HttpRequestBase request, DataType dataType, Object data) {
HttpEntity entiry = null;
if (dataType == DataType.FORM) {
request.setHeader("Content-Type", "application/x-www-form-urlencoded");
if (data != null) {
if (data instanceof Map, ?>) {
List valuePairs = new ArrayList();
Map bodys = (Map) data;
for (Map.Entry body : bodys.entrySet()) {
NameValuePair nameValuePair = new BasicNameValuePair(body.getKey(),
String.valueOf(body.getValue()));
valuePairs.add(nameValuePair);
}
entiry = new UrlEncodedFormEntity(valuePairs, Charset.forName("UTF-8"));
} else {
throw new RuntimeException("数据类型不正确");
}
}
} else if (dataType == DataType.JSON) {
request.setHeader("Content-Type", "application/json");
if (data != null) {
ContentType type = ContentType.create("application/json", Charset.forName("UTF-8"));
entiry = new StringEntity(data.toString(), type);
}
} else if (dataType == DataType.XML) {
request.setHeader("Content-Type", "application/xml");
if (data != null) {
ContentType type = ContentType.create("application/xml", Charset.forName("UTF-8"));
entiry = new StringEntity(data.toString(), type);
}
} else if (dataType == DataType.FILE) {
if (data != null) {
MultipartEntityBuilder multiPart = MultipartEntityBuilder.create();
if (data instanceof Map, ?>) {
Map files = (Map) data;
Iterator> iterator = files.entrySet().iterator();
while (iterator.hasNext()) {
Entry obj = (Entry) iterator.next();
String fieldName = obj.getKey();
Object val = obj.getValue();
if (val instanceof HttpFormFile) {
HttpFormFile file = (HttpFormFile) val;
InputStreamBody fileStream = null;
try {
fileStream = new InputStreamBody(new FileInputStream(file.getFilePath()),
file.getFileName());
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
multiPart.addPart(new FormBodyPart(fieldName, fileStream));
} else {
StringBody body = null;
try {
body = new StringBody(val.toString(), Charset.forName("UTF-8"));
} catch (Exception e) {
throw new RuntimeException(e);
}
multiPart.addPart(new FormBodyPart(fieldName, body));
}
}
entiry = multiPart.build();
} else {
throw new RuntimeException("数据类型不正确");
}
}
}
return entiry;
}
public static String delete(String user, String url) {
return delete(user, url, null);
}
public static String delete(String user, String url, Map headers) {
HttpDelete delete = new HttpDelete(url);
if (headers != null && headers.size() > 0) {
for (Map.Entry head : headers.entrySet()) {
delete.addHeader(head.getKey(), head.getValue());
}
}
return http(user, delete, null, null);
}
public static String put(String user, String url, DataType dataType, Object data) {
return put(user, url, null, dataType, data);
}
public static String put(String user, String url, Map headers, DataType dataType, Object data) {
HttpPut put = new HttpPut(url);
if (headers != null && headers.size() > 0) {
for (Map.Entry head : headers.entrySet()) {
put.addHeader(head.getKey(), head.getValue());
}
}
return http(user, put, dataType, data);
}
public static String get(String user, String url) {
return get(user, url, null);
}
public static String get(String user, String url, Map headers) {
HttpGet get = new HttpGet(url);
if (headers != null && headers.size() > 0) {
for (Map.Entry head : headers.entrySet()) {
get.addHeader(head.getKey(), head.getValue());
}
}
return http(user, get, null, null);
}
public static String post(String user, String url, DataType dataType, Object data) {
return post(user, url, null, dataType, data);
}
public static String post(String user, String url, Map headers, DataType dataType, Object data) {
HttpPost post = new HttpPost(url);
if (headers != null && headers.size() > 0) {
for (Map.Entry head : headers.entrySet()) {
post.addHeader(head.getKey(), head.getValue());
}
}
return http(user, post, dataType, data);
}
public static String getHttpResponseString(HttpResponse response) {
try {
return EntityUtils.toString(response.getEntity());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String getHttpResponseString(HttpResponse response, String charset) {
try {
return EntityUtils.toString(response.getEntity(), charset);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("serial")
public static class HttpException extends RuntimeException {
private String url;
private int code;
private String response;
public HttpException(String url, int code, String response) {
super(String.format("Error %d on %s", code, url));
this.url = url;
this.code = code;
this.response = response;
}
public String getUrl() {
return url;
}
public int getCode() {
return code;
}
public String getResponse() {
return response;
}
}
private static String http(String user, HttpRequestBase request, DataType dataType, Object data)
throws HttpException {
String response = null;
SSLContext sslContext = null;
try {
sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
// 信任所有
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
} catch (Exception e) {
throw new RuntimeException(e);
}
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
HttpClient client = HttpClients.custom().setSSLSocketFactory(sslsf).build();
try {
// request.addHeader(YiheTokenGenerator.getTokenHeaderName(), getSignature(user));
HttpEntity entiry = bulidHttpEntiry(request, dataType, data);
if (request instanceof HttpEntityEnclosingRequestBase) {
((HttpEntityEnclosingRequestBase) request).setEntity(entiry);
}
HttpResponse httpResponse = client.execute(request);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
Header locationHeader = httpResponse.getFirstHeader("location");
String location = null;
if (locationHeader != null) {
location = locationHeader.getValue();
try {
request.setURI(new URI(location));
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
response = http(user, request, dataType, data);
}
} else if (statusCode == HttpStatus.SC_OK) {
response = getHttpResponseString(httpResponse);
} else {
try {
logger.debug(httpResponse.getAllHeaders());
response = getHttpResponseString(httpResponse);
logger.debug(response);
} catch (Exception e) {
}
throw new HttpException(request.getURI().toString(), statusCode, response);
}
} catch (IOException ex) {
throw new RuntimeException(ex);
} finally {
request.releaseConnection();
}
return response;
}
}
public abstract class XmlUtil extends DomUtils {
private static final Log logger = LogFactory.getLog(XmlUtil.class);
public static Document buildDocument(String path)
throws ParserConfigurationException, SAXException, IOException {
return buildDocument(new FileInputStream(path));
}
public static Document buildDocument(InputStream stream)
throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder parser = dbf.newDocumentBuilder();
ErrorHandler handler = new SimpleSaxErrorHandler(logger);
parser.setErrorHandler(handler);
return parser.parse(stream);
}
public static Document buildDocumentForString(String xmlString)
throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder parser = dbf.newDocumentBuilder();
ErrorHandler handler = new SimpleSaxErrorHandler(logger);
parser.setErrorHandler(handler);
return parser.parse(new InputSource(new StringReader(xmlString)));
}
}