package com.boying.util;
|
|
import org.springframework.util.DigestUtils;
|
|
import java.io.*;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.net.URLConnection;
|
import java.util.Arrays;
|
import java.util.Map;
|
|
import static com.boying.util.MyX509TrustManager.httpsRequest;
|
|
public class HttpUtil {
|
|
static String ip3 = "https://pay.hebcz.cn";
|
static String ip4 = "https://110.249.165.78:443";
|
|
public static void main(String[] args) {
|
// String a = get("http://endu.hbshengcai.com:9092/ws_dingzhi/ws_dingzhi_lizheng/ws_open.ashx?carNo=冀A12345&barrierCode=000003");
|
// System.out.println(a);
|
//4.1
|
StringBuffer sbf = new StringBuffer();
|
StringBuffer s = new StringBuffer();
|
s.append("amt=0.01");
|
s.append("¬ifyUrl=http://192.168.0.145:8089/ffPay/result");
|
s.append("&payKey=df937eda27cb4a59a12347966678e232");
|
s.append("&payerName=冀A11111");
|
s.append("&payerNum=1623827528499");
|
s.append("&payerTypeCode=1304062100000001");
|
s.append("&reserve1=");
|
s.append("&reserve2=");
|
s.append("&reserve3=");
|
s.append("&returnUrl=");
|
sbf.append(s.toString());
|
s.append("&paySecret=9e3b2f387b5b4232a9c782affc9c36ed");
|
|
String encode = DigestUtils.md5DigestAsHex(s.toString().getBytes());
|
sbf.append("&sign="+encode);
|
sbf.append("&signType=MD5");
|
//String s1 = fromPost(sbf.toString(), ip3+"/pay/inpay");
|
String s1 = httpsRequest(ip4 + "/pay/inpay", "POST", sbf.toString());
|
System.out.println(s1);
|
|
// StringBuffer sbf = new StringBuffer();
|
// StringBuffer s = new StringBuffer();
|
// s.append("payCode=13000021E00000000570");
|
// s.append("&payKey=3ed6f127559e4f2f80e5e1cb0231b76b");
|
// //s.append("&sign=1b81e9236d5d36d4fa2788469be5c371");
|
// //s.append("&signType=MD5");
|
// sbf.append(s.toString());
|
// s.append("&paySecret=492f497a17cc41d19a757ae9d5809238");
|
// String encode = DigestUtils.md5DigestAsHex(s.toString().getBytes());
|
// sbf.append("&sign="+encode);
|
// sbf.append("&signType=MD5");
|
// String s1 = fromPost(sbf.toString(), "/pay/payUrlRedirect");
|
// System.out.println(s1);
|
|
//4.2
|
// StringBuffer sbf = new StringBuffer();
|
// StringBuffer s = new StringBuffer();
|
// s.append("payCode=13000021E00000000589");
|
// s.append("&payKey=3ed6f127559e4f2f80e5e1cb0231b76b");
|
// sbf.append(s.toString());
|
// s.append("&paySecret=492f497a17cc41d19a757ae9d5809238");
|
// String encode = DigestUtils.md5DigestAsHex(s.toString().getBytes());
|
// sbf.append("&sign="+encode);
|
// sbf.append("&signType=MD5");
|
// String s1 = fromPost(sbf.toString(), "/pay/checkEInvoiceInfo");
|
// System.out.println(s1);
|
|
//4.3
|
// StringBuffer sbf = new StringBuffer();
|
// StringBuffer s = new StringBuffer();
|
// s.append("payCode=13000021E00000000589");
|
// s.append("&payKey=3ed6f127559e4f2f80e5e1cb0231b76b");
|
// sbf.append(s.toString());
|
// s.append("&paySecret=492f497a17cc41d19a757ae9d5809238");
|
// String encode = DigestUtils.md5DigestAsHex(s.toString().getBytes());
|
// sbf.append("&sign="+encode);
|
// sbf.append("&signType=MD5");
|
// String s1 = fromPost(sbf.toString(), "/static/getInfoList");
|
// System.out.println(s1);
|
|
//4.4
|
// StringBuffer sbf = new StringBuffer();
|
// StringBuffer s = new StringBuffer();
|
// s.append("payKey=3ed6f127559e4f2f80e5e1cb0231b76b");
|
// s.append("&payerNum=a333e74eb9a043d08b1887b5979d6356");
|
// s.append("&payerTypeCode=130000451000011");
|
// sbf.append(s.toString());
|
// s.append("&paySecret=492f497a17cc41d19a757ae9d5809238");
|
// String encode = DigestUtils.md5DigestAsHex(s.toString().getBytes());
|
// sbf.append("&sign="+encode);
|
// sbf.append("&signType=MD5");
|
// String s1 = fromPost(sbf.toString(), "/pay/orderQuery");
|
// System.out.println(s1);
|
|
}
|
//static String ip = "http://njm6q2.natappfree.cc/heb-pay-web";
|
|
static String ip = "https://pay.hebcz.cn";
|
|
|
|
public static String fromPost(String params,String strURL) {
|
try {
|
URL url = new URL(strURL);// 创建连接
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
connection.setDoOutput(true);
|
connection.setDoInput(true);
|
connection.setUseCaches(false);
|
connection.setInstanceFollowRedirects(true);
|
connection.setRequestMethod("POST"); // 设置请求方式
|
connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
|
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); // 设置发送数据的格式
|
connection.connect();
|
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
|
out.write(params);
|
out.flush();
|
out.close();
|
|
int code = connection.getResponseCode();
|
InputStream is = null;
|
if (code == 200) {
|
is = connection.getInputStream();
|
} else {
|
is = connection.getErrorStream();
|
}
|
|
// 读取响应
|
int length = (int) connection.getContentLength();// 获取长度
|
if (length != -1) {
|
byte[] data = new byte[length];
|
byte[] temp = new byte[1024];
|
int readLen = 0;
|
int destPos = 0;
|
while ((readLen = is.read(temp)) > 0) {
|
System.arraycopy(temp, 0, data, destPos, readLen);
|
destPos += readLen;
|
}
|
String result = new String(data, "UTF-8"); // utf-8编码
|
return result;
|
}
|
|
BufferedReader bReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
String line, resultStr = "";
|
while (null != (line = bReader.readLine())) {
|
resultStr += line;
|
}
|
bReader.close();
|
return resultStr;
|
} catch (IOException e) {
|
return "error";
|
}
|
}
|
|
public static String map2Url(Map<String, Object> paramToMap) {
|
if (null == paramToMap || paramToMap.isEmpty()) {
|
return null;
|
}
|
StringBuffer url = new StringBuffer();
|
boolean isfist = true;
|
|
Object[] key = paramToMap.keySet().toArray();
|
Arrays.sort(key);
|
|
for (int i = 0; i < key.length; i++) {
|
if (isfist) {
|
isfist = false;
|
} else {
|
url.append("&");
|
}
|
Object o = paramToMap.get(key[i]);
|
url.append(key[i]).append("=").append(o);
|
}
|
|
// for (Map.Entry<String, Object> entry : paramToMap.entrySet()) {
|
// if (isfist) {
|
// isfist = false;
|
// } else {
|
// url.append("&");
|
// }
|
// url.append(entry.getKey()).append("=");
|
// Object value = entry.getValue();
|
// try {
|
// url.append(URLEncoder.encode(value.toString(), "UTF-8"));
|
// } catch (UnsupportedEncodingException e) {
|
// e.printStackTrace();
|
// }
|
// }
|
return url.toString();
|
}
|
|
public static String jsonPost2(String params,String strURL) {
|
try {
|
URL url = new URL(ip + strURL);// 创建连接
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
connection.setDoOutput(true);
|
connection.setDoInput(true);
|
connection.setUseCaches(false);
|
connection.setInstanceFollowRedirects(true);
|
connection.setRequestMethod("POST"); // 设置请求方式
|
connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
|
connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
|
connection.connect();
|
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
|
out.append(params);
|
out.flush();
|
out.close();
|
|
int code = connection.getResponseCode();
|
InputStream is = null;
|
if (code == 200) {
|
is = connection.getInputStream();
|
} else {
|
is = connection.getErrorStream();
|
}
|
|
// 读取响应
|
// int length = (int) connection.getContentLength();// 获取长度
|
// if (length != -1) {
|
// byte[] data = new byte[length];
|
// byte[] temp = new byte[512];
|
// int readLen = 0;
|
// int destPos = 0;
|
// while ((readLen = is.read(temp)) > 0) {
|
// System.arraycopy(temp, 0, data, destPos, readLen);
|
// destPos += readLen;
|
// }
|
// String result = new String(data, "UTF-8"); // utf-8编码
|
// return result;
|
// }
|
|
BufferedReader bReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
String line, resultStr = "";
|
while (null != (line = bReader.readLine())) {
|
resultStr += line;
|
}
|
bReader.close();
|
return resultStr;
|
} catch (IOException e) {
|
return "error";
|
}
|
}
|
|
/**
|
* 向指定URL发送GET方法的请求
|
*
|
*/
|
public static String get(String url) {
|
BufferedReader in = null;
|
try {
|
URL realUrl = new URL(url);
|
// 打开和URL之间的连接
|
URLConnection connection = realUrl.openConnection();
|
// 设置通用的请求属性
|
connection.setRequestProperty("accept", "*/*");
|
connection.setRequestProperty("connection", "Keep-Alive");
|
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
connection.setConnectTimeout(5000);
|
connection.setReadTimeout(5000);
|
// 建立实际的连接
|
connection.connect();
|
// 定义 BufferedReader输入流来读取URL的响应
|
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
StringBuffer sb = new StringBuffer();
|
String line;
|
while ((line = in.readLine()) != null) {
|
sb.append(line);
|
}
|
return sb.toString();
|
} catch (Exception e) {
|
}
|
// 使用finally块来关闭输入流
|
finally {
|
try {
|
if (in != null) {
|
in.close();
|
}
|
} catch (Exception e2) {
|
e2.printStackTrace();
|
}
|
}
|
return null;
|
}
|
}
|