オンライン ショッピング用の Java デスクトップ アプリを作成しようとしています。これまでのところ、ログインして製品を選択し、httpsurlconnect を使用してチェックアウトを選択できます。ただし、チェックアウト サイトが銀行の支払いゲートウェイにリダイレクトされた後。そして、リダイレクト後にリクエストを処理できないという応答が常に返されます。これは、支払いゲートウェイが双方向の相互ネゴシエーションを使用しているためだと思います (よくわかりません)。では、4 番目の要求で双方向の相互ネゴシエーションを作成するにはどうすればよいでしょうか。
インターネットで少し調べてみました。keystore.jks と truststore.jks が必要であることがわかりました。JDKには、サーバー証明書を受け入れることができるCAトラストストアが付属していると思います。クライアントキーと証明書を含む keystore.jks ファイルが必要です。自己署名証明書を作成すると、サーバーはそれを受け入れますか? サーバーユーザーは CA 証明書を検証済みであり、私はサーバーを制御できません。クライアントの keystore.jks を取得して、4 番目のリクエストでハンドシェイクを実装するにはどうすればよいですか。私の実装のサンプルコードを以下に示します。
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();
}