比较两种方式的form请求提交

[一]浏览器form表单提交

表单提交, 适用于浏览器提交。像常见的pc端的网银支付,用户在商户商城购买商品,支付时通过form表单提交到三方支付网关。
下面是一个form表单自动提交案例,将这段html输出到浏览器,会自动提交到目标action。

<form name="payForm" action='http://192.168.40.228:28080/app/userIdentify.do' method='POST' >
    <input type='hidden' name='version' value='B2C1.0'>
    <input type='hidden' name='tranData' value='PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iR0JLIj8+PEIyQ1JlcT48Y2FyZE5vPjE8L2NhcmRObz48Y3VzdG9tZXJOYW1lPkpvaG48L2N1c3RvbWVyTmFtZT48b3JkZXJObz5mZjYxYTk5OC0yN2I0LTQ1YzctOWE0Yi0yYmNlY2ZkNmJmN2E8L29yZGVyTm8+PGNlcnRObz4xMzA0MzQxOTgzMDEwNjc1MTE8L2NlcnRObz48dHJhblR5cGU+MTwvdHJhblR5cGU+PGJhbmtJZD4wPC9iYW5rSWQ+PG1vYmlsZT4xPC9tb2JpbGU+PC9CMkNSZXE+'> 
    <input type='hidden' name='signData' value='c3c50a27dac38c123c4be418b2273049'> 
    <input type='hidden' name='merchantId' value='M100001564'>
    <input type='submit' value='提交' />
    <script>document.forms['payForm'].submit();</script>
</form>

[二]服务端httppost

另一种是服务端点对点的post请求,此方式适用于接口调用。将请求数据以querystring的格式(a=val1&b=val2&c=val3&...)作为参数传(提)输(交)给服务端。此时注意要把Request的Content-Type为application/x-www-form-urlencoded。如下是一个工具方法:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 *
 * HTTP协议POST请求方法
 */
public static String httpMethodPost(String url, String params, String gb) {
    if (null == gb || "".equals(gb)) {
        gb = "UTF-8";
    }
    StringBuffer sb = new StringBuffer();
    URL urls;
    HttpURLConnection uc = null;
    BufferedReader in = null;
    try {
        urls = new URL(url);
        uc = (HttpURLConnection) urls.openConnection();
        uc.setRequestMethod("POST");
        uc.setDoOutput(true);
        uc.setDoInput(true);
        uc.setUseCaches(false);
        uc.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        uc.connect();
        if(!StringUtils.isBlank(params)){
            DataOutputStream out = new DataOutputStream(uc.getOutputStream());
            out.write(params.getBytes(gb));
            out.flush();
            out.close();
        }
        in = new BufferedReader(new InputStreamReader(uc.getInputStream(),
                gb));
        String readLine = "";
        while ((readLine = in.readLine()) != null) {
            sb.append(readLine);
        }
        if (in != null) {
            in.close();
        }
        if (uc != null) {
            uc.disconnect();
        }
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    } finally {
        if (uc != null) {
            uc.disconnect();
        }
    }
    return sb.toString();
}


调用方式:

String url = "http://192.168.40.228:28080/app/userIdentify.do";
String params = String.format("version=%s&tranData=%s&signData=%s&merchantId=%s", version, URLEncoder.encode(tranDataBase64, "GBK"), signData, merchantId);
String responseStr = HttpUtil.httpMethodPost(url, params, "GBK");

结束

猜你喜欢

转载自www.cnblogs.com/buguge/p/9198841.html