我正在尝试为在线购物创建 java 桌面应用程序。到目前为止,我可以使用 httpsurlconnect 登录、选择产品并选择结帐。但结帐网站重定向到银行支付网关后。而且我总是得到响应,无法在重定向后处理您的请求。我认为这是因为支付网关使用双向相互协商(不确定)。那么如何根据第四个请求创建双向相互协商。
我在互联网上做了一些研究。我发现它需要 keystore.jks 和 truststore.jks。我假设 JDK 带有可以接受服务器证书的 CA 信任库。我需要带有客户端密钥和证书的 keystore.jks 文件。如果我创建自签名证书,服务器会接受它吗?服务器用户经过验证的 CA 证书,我无法控制服务器。如何获取客户端 keystore.jks 并在第四次请求时实现握手。下面给出了我的实现的示例代码。
public void query(){
//request 1
String url_1 = "https://www.shop.com/login.do";
String ref_1 = "https://www.shop.com/";
Map<String, String> post = new HashMap<String, String>();
post.put("userName", var.userName);
post.put("password", var.password);
String new_post_1 = Generate_post(post);
Connect(url_1, ref_1, new_post_1);
//request 2
String url_2 = "https://www.shop.com/select_product.do";
String ref_2 = "https://www.shop.com/login.do";
Map<String, String> post = new HashMap<String, String>();
post.put("product", var.Name);
post.put("id", var.id);
post.put("price", var.price);
String new_post_2 = Generate_post(post);
Connect(url_2, ref_2, new_post_2);
//request 3
String url_3 = "https://www.shop.com/checkout.do";
String ref_3 = "https://www.shop.com/select_product.do";
Map<String, String> post = new HashMap<String, String>();
post.put("product", var.Name);
post.put("id", var.id);
post.put("total_price", var.total_price);
post.put("checkout", var.checkout);
String new_post_3 = Generate_post(post);
Connect(url_3, ref_3, new_post_3);
//request 4
String url_4 = "https://secure.payment.com/gateway.do";
String ref_4 = "https://www.shop.com/checkout.do";
Map<String, String> post = new HashMap<String, String>();
post.put("total_price", var.total_price);
post.put("bank", var.bank);
post.put("confirm", var.confirm);
String new_post_4 = Generate_post(post);
Connect(url_4, ref_4, new_post_4);
}
连接方法:
void Connect(String https_url, String referal, String query) throws IOException{
URL url = new URL(https_url);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
//set properties
HttpsURLConnection.setFollowRedirects(true);
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setUseCaches(false);
con.setAllowUserInteraction(false);
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0");
con.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
con.setRequestProperty("Accept-Language","en-us,en;q=0.5");
con.setRequestProperty("Connection","keep-alive");
con.setRequestProperty("charset","iso-8859-1");
con.setRequestProperty("Referer",referal);
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
con.setRequestProperty("Content-Length",String.valueOf(query.length()));
// Create Output streams
BufferedWriter outStream = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
outStream.write(query);
outStream.flush();
outStream.close();
if(con.getResponseCode()==HttpsURLConnection.HTTP_OK ){
// Create Input streams
InputStreamReader in = new InputStreamReader(con.getInputStream());
BufferedReader inStream = new BufferedReader(in, 1024*24);
String html;
StringBuffer page = new StringBuffer();
while((html = inStream.readLine()) != null) {
page.append(html+"\n"); }
inStream.close();
}else{
System.out.println(con.getHeaderFields());
InputStreamReader in = new InputStreamReader(con.getErrorStream());
BufferedReader inStream = new BufferedReader(in);
String html;
StringBuffer page = new StringBuffer();
while((html = inStream.readLine()) != null) {
page.append(html+"\n"); }
}
con.disconnect();
}