Http辅助工具类

//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<NameValuePair> valuePairs = new ArrayList<NameValuePair>();
					Map<String, String> bodys = (Map<String, String>) data;
					for (Map.Entry<String, String> 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<String, ?> files = (Map<String, ?>) data;
					Iterator<?> iterator = files.entrySet().iterator();
					while (iterator.hasNext()) {
						Entry<String, ?> obj = (Entry<String, ?>) 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<String, String> headers) {
		HttpDelete delete = new HttpDelete(url);
		if (headers != null && headers.size() > 0) {
			for (Map.Entry<String, String> 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<String, String> headers, DataType dataType, Object data) {
		HttpPut put = new HttpPut(url);
		if (headers != null && headers.size() > 0) {
			for (Map.Entry<String, String> 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<String, String> headers) {
		HttpGet get = new HttpGet(url);
		if (headers != null && headers.size() > 0) {
			for (Map.Entry<String, String> 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<String, String> headers, DataType dataType, Object data) {
		HttpPost post = new HttpPost(url);
		if (headers != null && headers.size() > 0) {
			for (Map.Entry<String, String> 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)));
	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值