{"id":334,"date":"2023-02-25T06:47:01","date_gmt":"2023-02-24T22:47:01","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=334"},"modified":"2023-04-30T14:50:15","modified_gmt":"2023-04-30T06:50:15","slug":"java-simulate-form-submission","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/java-simulate-form-submission\/","title":{"rendered":"Java\u6a21\u62dfForm\u8868\u5355\u63d0\u4ea4"},"content":{"rendered":"<p>\u4ee5\u652f\u4ed8\u5b9d\u4e3a\u4f8b\uff0c\u4f7f\u7528Java\u6a21\u62dfForm\u8868\u5355\u63d0\u4ea4<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">\/**\n * \u5efa\u7acb\u8bf7\u6c42\uff0c\u4ee5\u8868\u5355HTML\u5f62\u5f0f\u6784\u9020\uff08\u9ed8\u8ba4\uff09\n * @param sParaTemp \u8bf7\u6c42\u53c2\u6570\u6570\u7ec4\n * @param strMethod \u63d0\u4ea4\u65b9\u5f0f\u3002\u4e24\u4e2a\u503c\u53ef\u9009\uff1apost\u3001get\n * @param strButtonName \u786e\u8ba4\u6309\u94ae\u663e\u793a\u6587\u5b57\n * @return \u63d0\u4ea4\u8868\u5355HTML\u6587\u672c\n *\/\npublic static String buildRequest(Map&lt;String, String&gt; sParaTemp, String strMethod, String strButtonName) {\n    \/\/\u5f85\u8bf7\u6c42\u53c2\u6570\u6570\u7ec4\n    Map&lt;String, String&gt; sPara = buildRequestPara(sParaTemp);\n    List&lt;String&gt; keys = new ArrayList&lt;String&gt;(sPara.keySet());\n\n    StringBuffer sbHtml = new StringBuffer();\n\n    sbHtml.append(&quot;&lt;form id=\\&quot;alipaysubmit\\&quot; name=\\&quot;alipaysubmit\\&quot; action=\\&quot;&quot; + ALIPAY_GATEWAY_NEW\n                  + &quot;_input_charset=&quot; + AlipayConfig.input_charset + &quot;\\&quot; method=\\&quot;&quot; + strMethod\n                  + &quot;\\&quot;&gt;&quot;);\n\n    for (int i = 0; i &lt; keys.size(); i++) {\n        String name = (String) keys.get(i);\n        String value = (String) sPara.get(name);\n\n        sbHtml.append(&quot;&lt;input type=\\&quot;hidden\\&quot; name=\\&quot;&quot; + name + &quot;\\&quot; value=\\&quot;&quot; + value + &quot;\\&quot;\/&gt;&quot;);\n    }\n\n    \/\/submit\u6309\u94ae\u63a7\u4ef6\u8bf7\u4e0d\u8981\u542b\u6709name\u5c5e\u6027\n    sbHtml.append(&quot;&lt;input type=\\&quot;submit\\&quot; value=\\&quot;&quot; + strButtonName + &quot;\\&quot; style=\\&quot;display:none;\\&quot;&gt;&lt;\/form&gt;&quot;);\n    sbHtml.append(&quot;&lt;script&gt;document.forms[&#039;alipaysubmit&#039;].submit();&lt;\/script&gt;&quot;);\n\n    return sbHtml.toString();\n}\n\n\/**\n * \u5efa\u7acb\u8bf7\u6c42\uff0c\u4ee5\u8868\u5355HTML\u5f62\u5f0f\u6784\u9020\uff0c\u5e26\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd\n * @param sParaTemp \u8bf7\u6c42\u53c2\u6570\u6570\u7ec4\n * @param strMethod \u63d0\u4ea4\u65b9\u5f0f\u3002\u4e24\u4e2a\u503c\u53ef\u9009\uff1apost\u3001get\n * @param strButtonName \u786e\u8ba4\u6309\u94ae\u663e\u793a\u6587\u5b57\n * @param strParaFileName \u6587\u4ef6\u4e0a\u4f20\u7684\u53c2\u6570\u540d\n * @return \u63d0\u4ea4\u8868\u5355HTML\u6587\u672c\n *\/\npublic static String buildRequest(Map&lt;String, String&gt; sParaTemp, String strMethod, String strButtonName, String strParaFileName) {\n    \/\/\u5f85\u8bf7\u6c42\u53c2\u6570\u6570\u7ec4\n    Map&lt;String, String&gt; sPara = buildRequestPara(sParaTemp);\n    List&lt;String&gt; keys = new ArrayList&lt;String&gt;(sPara.keySet());\n\n    StringBuffer sbHtml = new StringBuffer();\n\n    sbHtml.append(&quot;&lt;form id=\\&quot;alipaysubmit\\&quot; name=\\&quot;alipaysubmit\\&quot;  enctype=\\&quot;multipart\/form-data\\&quot; action=\\&quot;&quot; + ALIPAY_GATEWAY_NEW\n                  + &quot;_input_charset=&quot; + AlipayConfig.input_charset + &quot;\\&quot; method=\\&quot;&quot; + strMethod\n                  + &quot;\\&quot;&gt;&quot;);\n\n    for (int i = 0; i &lt; keys.size(); i++) {\n        String name = (String) keys.get(i);\n        String value = (String) sPara.get(name);\n\n        sbHtml.append(&quot;&lt;input type=\\&quot;hidden\\&quot; name=\\&quot;&quot; + name + &quot;\\&quot; value=\\&quot;&quot; + value + &quot;\\&quot;\/&gt;&quot;);\n    }\n\n    sbHtml.append(&quot;&lt;input type=\\&quot;file\\&quot; name=\\&quot;&quot; + strParaFileName + &quot;\\&quot; \/&gt;&quot;);\n\n    \/\/submit\u6309\u94ae\u63a7\u4ef6\u8bf7\u4e0d\u8981\u542b\u6709name\u5c5e\u6027\n    sbHtml.append(&quot;&lt;input type=\\&quot;submit\\&quot; value=\\&quot;&quot; + strButtonName + &quot;\\&quot; style=\\&quot;display:none;\\&quot;&gt;&lt;\/form&gt;&quot;);\n\n    return sbHtml.toString();\n}\n\n\/**\n * \u751f\u6210\u8981\u8bf7\u6c42\u7ed9\u652f\u4ed8\u5b9d\u7684\u53c2\u6570\u6570\u7ec4\n * @param sParaTemp \u8bf7\u6c42\u524d\u7684\u53c2\u6570\u6570\u7ec4\n * @return \u8981\u8bf7\u6c42\u7684\u53c2\u6570\u6570\u7ec4\n *\/\nprivate static Map&lt;String, String&gt; buildRequestPara(Map&lt;String, String&gt; sParaTemp) {\n    \/\/\u9664\u53bb\u6570\u7ec4\u4e2d\u7684\u7a7a\u503c\u548c\u7b7e\u540d\u53c2\u6570\n    Map&lt;String, String&gt; sPara = AlipayCore.paraFilter(sParaTemp);\n    \/\/\u751f\u6210\u7b7e\u540d\u7ed3\u679c\n    String sign = buildRequestSign(sPara);\n\n    \/\/\u7b7e\u540d\u7ed3\u679c\u4e0e\u7b7e\u540d\u65b9\u5f0f\u52a0\u5165\u8bf7\u6c42\u63d0\u4ea4\u53c2\u6570\u7ec4\u4e2d\n    sPara.put(&quot;sign&quot;, sign);\n    sPara.put(&quot;sign_type&quot;, AlipayConfig.sign_type);\n\n    return sPara;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u652f\u4ed8\u5b9d\u4e3a\u4f8b\uff0c\u4f7f\u7528Java\u6a21\u62dfForm\u8868\u5355\u63d0\u4ea4 \/** * \u5efa\u7acb\u8bf7\u6c42\uff0c\u4ee5\u8868\u5355HTML\u5f62\u5f0f\u6784\u9020\uff08\u9ed8\u8ba4\uff09 * @p [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[109],"class_list":["post-334","post","type-post","status-publish","format-standard","hentry","category-java-basic","tag-form"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=334"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/334\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}