|
@@ -0,0 +1,400 @@
|
|
|
+package utils;
|
|
|
+
|
|
|
+
|
|
|
+import javax.net.ssl.HttpsURLConnection;
|
|
|
+import java.io.*;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLConnection;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.security.MessageDigest;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+public class HttpTool {
|
|
|
+ public static final String GET = "GET";
|
|
|
+ public static final String POST = "POST";
|
|
|
+ // 签名key
|
|
|
+ public final static String SIGNATURE = "sign";
|
|
|
+ // 签名方法key
|
|
|
+ public final static String SIGN_METHOD = "sign_type";
|
|
|
+ // =
|
|
|
+ public static final String QSTRING_EQUAL = "=";
|
|
|
+ // &
|
|
|
+ public static final String QSTRING_SPLIT = "&";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 向指定URL发送GET方法的请求
|
|
|
+ *
|
|
|
+ * @param address 发送请求的URL
|
|
|
+ * @param form 请求参数,请求参数应该是name1=value1&name2=value2的形式。
|
|
|
+ * @return URL所代表远程资源的响应
|
|
|
+ */
|
|
|
+
|
|
|
+ public static String sendGet(final String address, String form) {
|
|
|
+ String req = address + "?" + form;
|
|
|
+ System.out.println("请求:" + req);
|
|
|
+ StringBuilder response = new StringBuilder();
|
|
|
+ HttpURLConnection connection = null;
|
|
|
+ InputStream in = null;
|
|
|
+ try {
|
|
|
+ // 创建一个URL对象并传入地址
|
|
|
+ URL url = new URL(req);
|
|
|
+ // 用地址打开连接通道
|
|
|
+ connection = (HttpURLConnection) url.openConnection();
|
|
|
+ // 设置请求方式为get
|
|
|
+ connection.setRequestMethod("GET");
|
|
|
+ // 设置连接超时为8秒
|
|
|
+ connection.setConnectTimeout(8000);
|
|
|
+ // 设置读取超时为8秒
|
|
|
+ connection.setReadTimeout(8000);
|
|
|
+ connection.setRequestProperty("Charset", "utf-8");
|
|
|
+ // 设置可取
|
|
|
+// connection.setDoInput(true);
|
|
|
+// // 设置可读
|
|
|
+// connection.setDoOutput(true);
|
|
|
+// // 得到输入流
|
|
|
+ in = connection.getInputStream();
|
|
|
+
|
|
|
+ // 创建高效流对象
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
|
|
|
+ // 创建StringBuilder对象存储数据
|
|
|
+
|
|
|
+ String line;// 一次读取一行
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ response.append(line);// 得到的数据存入StringBuilder
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (connection != null) {// 通道不为null
|
|
|
+ connection.disconnect();// 关闭通道
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ if (in != null) {
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return response.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 向指定URL发送GET方法的请求
|
|
|
+ *
|
|
|
+ * @param url 发送请求的URL
|
|
|
+ * @param param 请求参数,请求参数应该是name1=value1&name2=value2的形式。
|
|
|
+ * @return URL所代表远程资源的响应
|
|
|
+ */
|
|
|
+
|
|
|
+ public static String sendGetCookie(String url, String param, String cookie) {
|
|
|
+ String result = "";
|
|
|
+ BufferedReader in = null;
|
|
|
+ try {
|
|
|
+ String urlName = url + "?" + param;
|
|
|
+ System.out.println(urlName);
|
|
|
+ URL realUrl = new URL(urlName);
|
|
|
+ // 打开和URL之间的连接
|
|
|
+ URLConnection conn = realUrl.openConnection();
|
|
|
+ // 设置通用的请求属性
|
|
|
+ conn.setRequestProperty("accept", "*/*");
|
|
|
+ conn.setRequestProperty("connection", "Keep-Alive");
|
|
|
+ conn.setRequestProperty("user-agent",
|
|
|
+ "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
|
|
|
+ conn.setRequestProperty("Cookie", cookie);
|
|
|
+ // 建立实际的连接
|
|
|
+ conn.connect();
|
|
|
+ // 获取所有响应头字段
|
|
|
+ Map<String, List<String>> map = conn.getHeaderFields();
|
|
|
+ // 遍历所有的响应头字段
|
|
|
+ // for (String key : map.keySet()) {
|
|
|
+ // System.out.println(key + "--->" + map.get(key));
|
|
|
+ // }
|
|
|
+ // 定义BufferedReader输入流来读取URL的响应
|
|
|
+ in = new BufferedReader(
|
|
|
+ new InputStreamReader(conn.getInputStream()));
|
|
|
+ String line;
|
|
|
+ while ((line = in.readLine()) != null) {
|
|
|
+ result += line;
|
|
|
+ // result += "/n" + line;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println("发送GET请求出现异常!" + e);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ // 使用finally块来关闭输入流
|
|
|
+ finally {
|
|
|
+ try {
|
|
|
+ if (in != null) {
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ } catch (IOException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 向指定URL发送POST方法的请求
|
|
|
+ *
|
|
|
+ * @param url 发送请求的URL
|
|
|
+ * @param param 请求参数,请求参数应该是name1=value1&name2=value2的形式。
|
|
|
+ * @return URL所代表远程资源的响应
|
|
|
+ */
|
|
|
+ public static String sendPost(String url, String param) {
|
|
|
+ PrintWriter out = null;
|
|
|
+ BufferedReader in = null;
|
|
|
+ String result = "";
|
|
|
+ try {
|
|
|
+ URL realUrl = new URL(url);
|
|
|
+ // 打开和URL之间的连接
|
|
|
+ URLConnection conn = realUrl.openConnection();
|
|
|
+ // 设置通用的请求属性
|
|
|
+ conn.setRequestProperty("accept", "*/*");
|
|
|
+ conn.setRequestProperty("connection", "Keep-Alive");
|
|
|
+ conn.setRequestProperty("user-agent",
|
|
|
+ "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
|
|
|
+ // 发送POST请求必须设置如下两行
|
|
|
+ conn.setDoOutput(true);
|
|
|
+ conn.setDoInput(true);
|
|
|
+ // 获取URLConnection对象对应的输出流
|
|
|
+ out = new PrintWriter(conn.getOutputStream());
|
|
|
+ // 发送请求参数
|
|
|
+ out.print(param);
|
|
|
+ // flush输出流的缓冲
|
|
|
+ out.flush();
|
|
|
+ // 定义BufferedReader输入流来读取URL的响应
|
|
|
+ in = new BufferedReader(
|
|
|
+ new InputStreamReader(conn.getInputStream()));
|
|
|
+ String line;
|
|
|
+ while ((line = in.readLine()) != null) {
|
|
|
+ result += line;
|
|
|
+ // result += "/n" + line;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println("发送POST请求出现异常!" + e);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ // 使用finally块来关闭输出流、输入流
|
|
|
+ finally {
|
|
|
+ try {
|
|
|
+ if (out != null) {
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+ if (in != null) {
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ } catch (IOException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 向指定URL发送POST方法的请求
|
|
|
+ *
|
|
|
+ * @param url 发送请求的URL
|
|
|
+ * @param param 请求参数,请求参数应该是name1=value1&name2=value2的形式。
|
|
|
+ * @return URL所代表远程资源的响应
|
|
|
+ */
|
|
|
+ public static String sendPostCookie(String url, String param, String cookie) {
|
|
|
+ PrintWriter out = null;
|
|
|
+ BufferedReader in = null;
|
|
|
+ String result = "";
|
|
|
+ try {
|
|
|
+ URL realUrl = new URL(url);
|
|
|
+ // 打开和URL之间的连接
|
|
|
+ URLConnection conn = realUrl.openConnection();
|
|
|
+ // 设置通用的请求属性
|
|
|
+ conn.setRequestProperty("accept", "*/*");
|
|
|
+ conn.setRequestProperty("connection", "Keep-Alive");
|
|
|
+ conn.setRequestProperty("user-agent",
|
|
|
+ "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
|
|
|
+ conn.setRequestProperty("Cookie", cookie);
|
|
|
+ // 发送POST请求必须设置如下两行
|
|
|
+ conn.setDoOutput(true);
|
|
|
+ conn.setDoInput(true);
|
|
|
+ // 获取URLConnection对象对应的输出流
|
|
|
+ out = new PrintWriter(conn.getOutputStream());
|
|
|
+ // 发送请求参数
|
|
|
+ out.print(param);
|
|
|
+ // flush输出流的缓冲
|
|
|
+ out.flush();
|
|
|
+ // 定义BufferedReader输入流来读取URL的响应
|
|
|
+ in = new BufferedReader(
|
|
|
+ new InputStreamReader(conn.getInputStream()));
|
|
|
+ String line;
|
|
|
+ while ((line = in.readLine()) != null) {
|
|
|
+ result += line;
|
|
|
+ // result += "/n" + line;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println("发送POST请求出现异常!" + e);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ // 使用finally块来关闭输出流、输入流
|
|
|
+ finally {
|
|
|
+ try {
|
|
|
+ if (out != null) {
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+ if (in != null) {
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ } catch (IOException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * https访问
|
|
|
+ */
|
|
|
+ public static String doHttps(String httpurl, String method, String param) {
|
|
|
+ try {
|
|
|
+ URL reqURL = new URL(httpurl + param); // 创建URL对象
|
|
|
+ HttpsURLConnection httpsConn = (HttpsURLConnection) reqURL
|
|
|
+ .openConnection();
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 下面这段代码实现向Web页面发送数据,实现与网页的交互访问 httpsConn.setDoOutput(true);
|
|
|
+ * OutputStreamWriter out = new
|
|
|
+ * OutputStreamWriter(huc.getOutputStream(), "8859_1"); out.write(
|
|
|
+ * "……" ); out.flush(); out.close();
|
|
|
+ */
|
|
|
+
|
|
|
+ // 取得该连接的输入流,以读取响应内容
|
|
|
+ InputStreamReader insr = new InputStreamReader(
|
|
|
+ httpsConn.getInputStream());
|
|
|
+
|
|
|
+ // 读取服务器的响应内容并显示
|
|
|
+ int respInt = insr.read();
|
|
|
+ while (respInt != -1) {
|
|
|
+ System.out.print((char) respInt);
|
|
|
+ respInt = insr.read();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // TODO: handle exception
|
|
|
+ }
|
|
|
+
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 除去请求要素中的空值和签名参数
|
|
|
+ *
|
|
|
+ * @param para 请求要素
|
|
|
+ * @return 去掉空值与签名参数后的请求要素
|
|
|
+ */
|
|
|
+ public static Map<String, String> paraFilter(Map<String, String> para) {
|
|
|
+
|
|
|
+ Map<String, String> result = new HashMap<String, String>();
|
|
|
+
|
|
|
+ if (para == null || para.size() <= 0) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (String key : para.keySet()) {
|
|
|
+ String value = para.get(key);
|
|
|
+ if (value == null || value.equals("")
|
|
|
+ || key.equalsIgnoreCase(SIGNATURE)
|
|
|
+ || key.equalsIgnoreCase(SIGN_METHOD)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ result.put(key, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把请求要素按照“参数=参数值”的模式用“&”字符拼接成字符串
|
|
|
+ *
|
|
|
+ * @param para 请求要素
|
|
|
+ * @param sort 是否需要根据key值作升序排列
|
|
|
+ * @param encode 是否需要URL编码
|
|
|
+ * @return 拼接成的字符串
|
|
|
+ */
|
|
|
+ public static String createLinkString(Map<String, String> para,
|
|
|
+ boolean sort, boolean encode) {
|
|
|
+
|
|
|
+ List<String> keys = new ArrayList<String>(para.keySet());
|
|
|
+
|
|
|
+ if (sort)
|
|
|
+ Collections.sort(keys);
|
|
|
+
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (int i = 0; i < keys.size(); i++) {
|
|
|
+ String key = keys.get(i);
|
|
|
+ String value = para.get(key);
|
|
|
+
|
|
|
+ if (encode) {
|
|
|
+ try {
|
|
|
+ value = URLEncoder.encode(value, "utf-8");
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (i == keys.size() - 1) {// 拼接时,不包括最后一个&字符
|
|
|
+ sb.append(key).append(QSTRING_EQUAL).append(value);
|
|
|
+ } else {
|
|
|
+ sb.append(key).append(QSTRING_EQUAL).append(value)
|
|
|
+ .append(QSTRING_SPLIT);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 对传入的参数进行MD5摘要
|
|
|
+ *
|
|
|
+ * @param str 需进行MD5摘要的数据
|
|
|
+ * @return MD5摘要值
|
|
|
+ */
|
|
|
+ public static String md5Summary(String str) {
|
|
|
+
|
|
|
+ if (str == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ MessageDigest messageDigest = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ messageDigest = MessageDigest.getInstance("MD5");
|
|
|
+ messageDigest.reset();
|
|
|
+ messageDigest.update(str.getBytes(StandardCharsets.UTF_8));
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
+
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
+ byte[] byteArray = messageDigest.digest();
|
|
|
+
|
|
|
+ StringBuffer md5StrBuff = new StringBuffer();
|
|
|
+
|
|
|
+ for (int i = 0; i < byteArray.length; i++) {
|
|
|
+ if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
|
|
|
+ md5StrBuff.append("0").append(
|
|
|
+ Integer.toHexString(0xFF & byteArray[i]));
|
|
|
+ else
|
|
|
+ md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
|
|
|
+ }
|
|
|
+
|
|
|
+ return md5StrBuff.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String result = sendGet("http://localhost:8080/agentPay", "account_type=01&amount=ds&appid=4110&banck_name=dsf&card_number=fsds&customer_name=sdfsdf&customer_type=01&mchorder_id=1505280293071&pay_password=123&sign=cf587510835e4f2d2fbdf43b094a13e4");
|
|
|
+ System.out.println(result);
|
|
|
+ }
|
|
|
+}
|