java客户端应用程序中的双向相互协商

时间:2013-01-10 11:08:46

标签: java ssl httpsurlconnection

我正在尝试为在线购物创建Java桌面应用。到目前为止,我可以登录,选择产品,然后选择退房 使用httpsurlconnect。但结账后网站重定向到银行支付网关。我总是得到回复,无法在重定向后处理您的请求。 我认为这是因为支付网关使用双向相互协商(不确定)。那么如何在第四次请求中创建双向相互协商。

我在互联网上做了一些研究。我发现它需要keystore.jks和truststore.jks。我认为JDK附带了可以接受服务器证书的CA truststore。 我需要带有客户端密钥和证书的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);
        }

Connnect方法:

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();
}

0 个答案:

没有答案