|
|
@@ -0,0 +1,271 @@
|
|
|
+package com.uas.plugins.maven.ops.diff.util;
|
|
|
+
|
|
|
+import javax.net.ssl.*;
|
|
|
+import java.io.*;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.charset.Charset;
|
|
|
+import java.security.SecureRandom;
|
|
|
+import java.security.cert.CertificateException;
|
|
|
+import java.security.cert.X509Certificate;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by Pro1 on 2017/6/15.
|
|
|
+ */
|
|
|
+public class HttpUtils {
|
|
|
+
|
|
|
+ private static final Charset defaultCharset = Charset.forName("UTF-8");
|
|
|
+
|
|
|
+ private static HttpURLConnection getDefaultHttpURLConnection(URL url) throws IOException{
|
|
|
+ HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
|
|
|
+ urlConn.setDoOutput(true);
|
|
|
+ urlConn.setDoInput(true);
|
|
|
+ urlConn.setUseCaches(false);
|
|
|
+ urlConn.setInstanceFollowRedirects(true);
|
|
|
+ return urlConn;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static HttpsURLConnection getDefaultHttpsURLConnection(URL url) throws Exception{
|
|
|
+ HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection();
|
|
|
+ SSLContext ctx = SSLContext.getInstance("TLS");
|
|
|
+ ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
|
|
|
+ SSLContext.setDefault(ctx);
|
|
|
+ urlConn.setDoOutput(true);
|
|
|
+ urlConn.setDoInput(true);
|
|
|
+ urlConn.setUseCaches(false);
|
|
|
+ urlConn.setInstanceFollowRedirects(true);
|
|
|
+ urlConn.setHostnameVerifier(new HostnameVerifier() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean verify(String hostname, SSLSession session) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return urlConn;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static HttpURLConnection getDefaultURLConnection(String requestUrl) throws Exception{
|
|
|
+ URL url = new URL(requestUrl);
|
|
|
+ if(requestUrl.startsWith("https")) {
|
|
|
+ return getDefaultHttpsURLConnection(url);
|
|
|
+ }
|
|
|
+ return getDefaultHttpURLConnection(url);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * HTTP GET请求
|
|
|
+ * @param getUrl
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static ResponseWrap get(String getUrl) {
|
|
|
+ HttpURLConnection urlConn = null;
|
|
|
+ try {
|
|
|
+ urlConn = getDefaultURLConnection(getUrl);
|
|
|
+ urlConn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
|
|
+ urlConn.setRequestMethod("GET");
|
|
|
+ return new ResponseWrap(urlConn);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return new ResponseWrap(false, e.getMessage());
|
|
|
+ } finally {
|
|
|
+ if (urlConn != null) {
|
|
|
+ urlConn.disconnect();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * HTTP GET请求
|
|
|
+ * @param getUrl
|
|
|
+ * @param data
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static ResponseWrap get(String getUrl, Map<String, Object> data) {
|
|
|
+ return get(getUrl + (getUrl.contains("?") ? "&" : "?") + getFormData(data));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文件下载
|
|
|
+ * @param downloadUrl
|
|
|
+ * @param targetFile
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static ResponseWrap download(String downloadUrl, File targetFile) {
|
|
|
+ HttpURLConnection urlConn = null;
|
|
|
+ try {
|
|
|
+ urlConn = getDefaultURLConnection(downloadUrl);
|
|
|
+ urlConn.setRequestMethod("GET");
|
|
|
+ return new ResponseWrap(urlConn, targetFile);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return new ResponseWrap(false, e.getMessage());
|
|
|
+ } finally {
|
|
|
+ if (urlConn != null) {
|
|
|
+ urlConn.disconnect();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文件下载
|
|
|
+ * @param getUrl
|
|
|
+ * @param data
|
|
|
+ * @param targetFile
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static ResponseWrap download(String getUrl, Map<String, Object> data, File targetFile) {
|
|
|
+ return download(getUrl + (getUrl.contains("?") ? "&" : "?") + getFormData(data), targetFile);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * HTTP POST请求
|
|
|
+ * @param postUrl
|
|
|
+ * @param formData
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static ResponseWrap post(String postUrl, Map<String, Object> formData) {
|
|
|
+ HttpURLConnection urlConn = null;
|
|
|
+ try {
|
|
|
+ urlConn = getDefaultURLConnection(postUrl);
|
|
|
+ urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
|
|
+ urlConn.setRequestMethod("POST");
|
|
|
+ StreamUtils.copy(getFormData(formData), defaultCharset, urlConn.getOutputStream());
|
|
|
+ return new ResponseWrap(urlConn);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return new ResponseWrap(false, e.getMessage());
|
|
|
+ } finally {
|
|
|
+ if (null != urlConn) {
|
|
|
+ urlConn.disconnect();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文件上传
|
|
|
+ * @param postUrl
|
|
|
+ * @param formData
|
|
|
+ * @param sourceFile
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static ResponseWrap upload(String postUrl, Map<String, Object> formData, File sourceFile) {
|
|
|
+ HttpURLConnection urlConn = null;
|
|
|
+ DataOutputStream out = null;
|
|
|
+ FileInputStream in = null;
|
|
|
+ String end = "\r\n";
|
|
|
+ String twoHyphens = "--";
|
|
|
+ String boundary = "*****";
|
|
|
+ try {
|
|
|
+ in = new FileInputStream(sourceFile);
|
|
|
+ urlConn = getDefaultURLConnection(postUrl);
|
|
|
+ urlConn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
|
|
|
+ urlConn.setRequestMethod("POST");
|
|
|
+ out = new DataOutputStream(urlConn.getOutputStream());
|
|
|
+ if (null != formData) {
|
|
|
+ for(String field:formData.keySet()) {
|
|
|
+ out.writeBytes(twoHyphens + boundary + end);
|
|
|
+ out.writeBytes("Content-Disposition: form-data; name=\"" + field + "\"" + end);
|
|
|
+ out.writeBytes(end);
|
|
|
+ out.writeBytes(String.valueOf(formData.get(field)));
|
|
|
+ out.writeBytes(end);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ out.writeBytes(twoHyphens + boundary + end);
|
|
|
+ out.writeBytes("Content-Disposition: form-data; Content-Type:application/octet-stream; name=\"file\";filename=\"" + sourceFile.getName() + "\"" + end);
|
|
|
+ out.writeBytes(end);
|
|
|
+ StreamUtils.copy(in, out);
|
|
|
+ out.writeBytes(end);
|
|
|
+ out.writeBytes(twoHyphens + boundary + twoHyphens + end);
|
|
|
+ out.flush();
|
|
|
+ return new ResponseWrap(urlConn);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return new ResponseWrap(false, e.getMessage());
|
|
|
+ } finally {
|
|
|
+ if (null != urlConn) {
|
|
|
+ urlConn.disconnect();
|
|
|
+ }
|
|
|
+ IOUtils.closeQuietly(in, out);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getFormData(Map<String, Object> data) {
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ for (String key : data.keySet())
|
|
|
+ if (data.get(key) != null) {
|
|
|
+ try {
|
|
|
+ sb.append(key).append('=').append(URLEncoder.encode(data.get(key).toString(), "UTF-8")).append('&');
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return sb.substring(0, sb.length() - 1).toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static class ResponseWrap {
|
|
|
+ private boolean success;
|
|
|
+ private String content;
|
|
|
+
|
|
|
+ public ResponseWrap(boolean success, String content) {
|
|
|
+ super();
|
|
|
+ this.success = success;
|
|
|
+ this.content = content;
|
|
|
+ }
|
|
|
+
|
|
|
+ public ResponseWrap(HttpURLConnection urlConn) throws IOException{
|
|
|
+ super();
|
|
|
+ this.success = urlConn.getResponseCode() == 200;
|
|
|
+ this.content = StreamUtils.copyToString((this.success ? urlConn.getInputStream() : urlConn.getErrorStream()), defaultCharset);
|
|
|
+ }
|
|
|
+
|
|
|
+ public ResponseWrap(HttpURLConnection urlConn, File targetFile) throws IOException{
|
|
|
+ super();
|
|
|
+ this.success = urlConn.getResponseCode() == 200;
|
|
|
+ if (this.success) {
|
|
|
+ if (!targetFile.getParentFile().exists()) {
|
|
|
+ targetFile.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+ StreamUtils.copy(urlConn.getInputStream(), new FileOutputStream(targetFile));
|
|
|
+ } else {
|
|
|
+ this.content = StreamUtils.copyToString(urlConn.getErrorStream(), defaultCharset);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean isSuccess() {
|
|
|
+ return success;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setSuccess(boolean success) {
|
|
|
+ this.success = success;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getContent() {
|
|
|
+ return content;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setContent(String content) {
|
|
|
+ this.content = content;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static class DefaultTrustManager implements X509TrustManager {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public X509Certificate[] getAcceptedIssuers() {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|