Browse Source

上传文件

陈士柏 2 years ago
parent
commit
09a3d2cfd8
1 changed files with 267 additions and 0 deletions
  1. 267 0
      mhotel/src/com/happy/Until/HttpUtils.java

+ 267 - 0
mhotel/src/com/happy/Until/HttpUtils.java

@@ -0,0 +1,267 @@
+package com.happy.Until;
+
+import com.alibaba.fastjson.JSONObject;
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+import com.happy.Model.weixin.WeiXinUtil;
+import com.happy.common.http.MyX509TrustManager;
+import com.happy.common.wx.WxConfig;
+import org.apache.http.HttpEntity;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.ClientProtocolException;
+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.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.conn.ssl.SSLContexts;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.util.EntityUtils;
+
+import javax.net.ssl.*;
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.security.KeyStore;
+import java.text.SimpleDateFormat;
+import java.util.*;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * http post 提交 和 get请求
+ *
+ * @author QT-666
+ *
+ */
+public class HttpUtils {
+
+	private static Integer READ_TIMEOUT = WxConfig.readTimeout;
+	private static RequestConfig requestConfig = RequestConfig.custom()
+			.setSocketTimeout(15000).setConnectTimeout(15000)
+			.setConnectionRequestTimeout(15000).build();
+
+	public static String get(String url, Map<String, String> params) {
+		CloseableHttpClient httpClient = null;
+		HttpGet httpGet = null;
+		String re = "";
+		try {
+			httpClient = HttpClients.createDefault();
+			RequestConfig requestConfig = RequestConfig.custom()
+					.setSocketTimeout(20000).setConnectTimeout(20000).build();
+			String ps = "";
+			for (String pKey : params.keySet()) {
+				if (!"".equals(ps)) {
+					ps = ps + "&";
+				}
+				ps = pKey + "=" + params.get(pKey);
+			}
+			if (!"".equals(ps)) {
+				url = url + "?" + ps;
+			}
+			httpGet = new HttpGet(url);
+			httpGet.setConfig(requestConfig);
+			CloseableHttpResponse response = httpClient.execute(httpGet);
+			HttpEntity httpEntity = response.getEntity();
+			re = EntityUtils.toString(httpEntity, "utf-8");
+		} catch (ClientProtocolException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		} finally {
+			try {
+				if (httpGet != null) {
+					httpGet.releaseConnection();
+				}
+				if (httpClient != null) {
+					httpClient.close();
+				}
+			} catch (IOException e) {
+				e.printStackTrace();
+			}
+		}
+		return re;
+	}
+
+	/**
+	 * 发送http post请求
+	 *
+	 * @param
+	 *
+	 * @param
+	 *
+	 */
+	public static String post(String url, Map<String, String> params) throws IOException {
+		CloseableHttpClient httpClient = null;
+		HttpPost httpPost = null;
+		String re = "";
+		try {
+			httpClient = HttpClients.createDefault();
+			RequestConfig requestConfig = RequestConfig.custom()
+					.setSocketTimeout(20000).setConnectTimeout(20000).build();
+			httpPost = new HttpPost(url);
+			httpPost.setConfig(requestConfig);
+			List<NameValuePair> ps = new ArrayList<NameValuePair>();
+			for (String pKey : params.keySet()) {
+				ps.add(new BasicNameValuePair(pKey, params.get(pKey)));
+			}
+			httpPost.setEntity(new UrlEncodedFormEntity(ps));
+			CloseableHttpResponse response = httpClient.execute(httpPost);
+			HttpEntity httpEntity = response.getEntity();
+			re = EntityUtils.toString(httpEntity, "utf-8");
+		} catch (ClientProtocolException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		} finally {
+			try {
+				if (httpPost != null) {
+					httpPost.releaseConnection();
+				}
+				if (httpClient != null) {
+					httpClient.close();
+				}
+			} catch (IOException e) {
+				e.printStackTrace();
+			}
+		}
+		return re;
+	}
+
+	/**
+	 * 发送post请求Https,参数是字符串
+	 *
+	 * @param
+	 * @return
+	 */
+	public static String post(String url, String body) throws Exception {
+		String str = "";
+		CloseableHttpClient httpClient = null;
+		HttpPost httpPost = null;
+		try {
+			httpClient = HttpClients.createDefault();
+			RequestConfig requestConfig = RequestConfig.custom()
+					.setSocketTimeout(20000).setConnectTimeout(20000).build();
+			httpPost = new HttpPost(url);
+			httpPost.setConfig(requestConfig);
+			httpPost.setEntity(new StringEntity(body, "utf-8"));
+			CloseableHttpResponse response = httpClient.execute(httpPost);
+			HttpEntity httpEntity = response.getEntity();
+			str = EntityUtils.toString(httpEntity, "utf-8");
+
+		} catch (ClientProtocolException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		} finally {
+			try {
+				if (httpPost != null) {
+					httpPost.releaseConnection();
+				}
+				if (httpClient != null) {
+					httpClient.close();
+				}
+			} catch (IOException e) {
+				e.printStackTrace();
+			}
+		}
+		return str;
+	}
+
+
+	public static String post2(String url, String body) throws Exception {
+		//商户号
+		//微信公众平台:“微信支付”--》“商户信息”--》“商户号”,将该值赋值给partner
+		String partner = WeiXinUtil.account;
+
+		//p12证书的位置
+		//微信公众平台:“微信支付”--》“商户信息”--》“交易数据”--》“详情请登录微信支付商户平台查看”(登录)--》“API安全”--》“API证书”--》“下载证书”
+		//下载证书后将apiclient_cert.p12放在src目录下面(出于安全考虑,请自行下载自己的证书)
+		String apiclient_certLocation = "/usr/mbin2/apiclient_cert.p12";
+		String str = "";
+		CloseableHttpClient httpClient = null;
+		HttpPost httpPost = null;
+		KeyStore keyStore  = KeyStore.getInstance("PKCS12");
+		FileInputStream instream = new FileInputStream(new File(apiclient_certLocation));//P12文件目录
+
+		try {
+			keyStore.load(instream, partner.toCharArray());
+			HttpsURLConnection httpsUrlConnection = null;
+			//创建https请求证书
+			SSLContext sslcontext = SSLContexts.custom()
+					.loadKeyMaterial(keyStore, partner.toCharArray())//这里也是写密码的
+					.build();
+			SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
+					sslcontext,
+					new String[] { "TLSv1" },
+					null,
+					SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
+			httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
+			RequestConfig requestConfig = RequestConfig.custom()
+					.setSocketTimeout(20000).setConnectTimeout(20000).build();
+			httpPost = new HttpPost(url);
+			httpPost.setConfig(requestConfig);
+			httpPost.setEntity(new StringEntity(body, "utf-8"));
+
+			CloseableHttpResponse response = httpClient.execute(httpPost);
+			HttpEntity httpEntity = response.getEntity();
+			str = EntityUtils.toString(httpEntity, "utf-8");
+
+		} catch (ClientProtocolException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		} finally {
+			try {
+				if (httpPost != null) {
+					httpPost.releaseConnection();
+				}
+				if (httpClient != null) {
+					httpClient.close();
+				}
+				instream.close();
+			} catch (IOException e) {
+				e.printStackTrace();
+			}
+		}
+		return new String(str.getBytes("iso-8859-1"));
+	}
+
+
+	public static String get(String strURL) throws Exception {
+		URL url = new URL(strURL);
+		HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
+		httpConn.setRequestMethod("GET");
+		httpConn.connect();
+
+		BufferedReader reader = new BufferedReader(new InputStreamReader(
+				httpConn.getInputStream(), "utf-8"));
+		String line;
+		StringBuffer buffer = new StringBuffer();
+		while ((line = reader.readLine()) != null) {
+			buffer.append(line);
+		}
+		reader.close();
+		httpConn.disconnect();
+
+		return buffer.toString();
+	}
+
+	public static void main(String[] args) throws Exception {
+		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
+		String time = df.format(new Date());
+		Map<String, String> req = new HashMap<>();
+		req.put("operator_id", "000000002");
+		req.put("operator_secret", "SBC88d0ItR3w6oCTY");
+		req.put("data_secret", "Epc3hhs0imtVhPSu");
+		req.put("data_secret_iv", "r57m1sr8Tg2E302L");
+		req.put("sign_key", "ikAhygeGCRnEdhjgBKf");
+		req.put("timeStamp", time);
+		req.put("seq", "0001");
+		String msg = post("http://183.129.139.222:10022/ems-share-api/queryToken", req);
+		System.out.println("aaa: "+msg);
+	}
+}