{"id":35788,"date":"2024-11-26T17:48:34","date_gmt":"2024-11-26T09:48:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35788\/"},"modified":"2024-11-26T17:48:34","modified_gmt":"2024-11-26T09:48:34","slug":"https-%e6%8e%a5%e5%8f%a3%e5%8f%91%e9%80%81%e6%95%b0%e6%8d%ae%e6%97%b6%e5%87%ba%e7%8e%b0%e7%a9%ba%e6%8c%87%e9%92%88%e5%bc%82%e5%b8%b8%ef%bc%8c%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/35788\/","title":{"rendered":"HTTPS \u63a5\u53e3\u53d1\u9001\u6570\u636e\u65f6\u51fa\u73b0\u7a7a\u6307\u9488\u5f02\u5e38\uff0c\u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173073006326707.jpg\" class=\"aligncenter\" title=\"HTTPS \u63a5\u53e3\u53d1\u9001\u6570\u636e\u65f6\u51fa\u73b0\u7a7a\u6307\u9488\u5f02\u5e38\uff0c\u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" alt=\"HTTPS \u63a5\u53e3\u53d1\u9001\u6570\u636e\u65f6\u51fa\u73b0\u7a7a\u6307\u9488\u5f02\u5e38\uff0c\u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>post \u5411 https \u63a5\u53e3\u53d1\u9001\u6570\u636e\uff0c\u90e8\u7f72\u5230\u670d\u52a1\u5668\u540e\u8fd0\u884c\u4e00\u6bb5\u65f6\u95f4\u540e\u62a5\u7a7a\u6307\u9488\u7684\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<p>\u95ee\u9898\u4e2d\u7ed9\u51fa\u7684\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c\u5f02\u5e38\u51fa\u73b0\u5728\u521b\u5efa outputstreamwriter \u5bf9\u8c61\u65f6\uff0c\u5373\uff1a<\/p>\n<pre>outputstreamwriter out = new outputstreamwriter(conn.getoutputstream(), \"utf-8\");<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b64\u884c\u4ee3\u7801\u4e2d\uff0cconn \u4e3a\u4e00\u4e2a httpsurlconnection \u5bf9\u8c61\uff0c\u8c03\u7528\u5176 getoutputstream() \u65b9\u6cd5\u65f6\u51fa\u73b0\u4e86\u7a7a\u6307\u9488\u5f02\u5e38\u3002<\/p>\n<p><strong>\u539f\u56e0\uff1a<\/strong><\/p>\n<p>\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u51fa\u73b0\u8fd9\u79cd\u60c5\u51b5\u53ef\u80fd\u662f\u7531\u4e8e\u4ee5\u4e0b\u539f\u56e0\uff1a<\/p>\n<ul>\n<li>conn \u5bf9\u8c61\u672a\u6b63\u786e\u5efa\u7acb\u8fde\u63a5\u3002<\/li>\n<li>\u670d\u52a1\u5668\u7aef\u53d1\u751f\u5f02\u5e38\u5bfc\u81f4\u65e0\u6cd5\u83b7\u53d6\u8f93\u51fa\u6d41\u3002<\/li>\n<\/ul>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u5efa\u8bae\u4f7f\u7528\u4e00\u4e2a\u5355\u72ec\u7684\u65b9\u6cd5\u4e13\u95e8\u7528\u4e8e\u53d1\u9001 post \u8bf7\u6c42\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u5199\u6b7b url \u548c\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4fee\u6539\u540e\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>public static String sendPost(String url, String postData) {\n    HttpsURLConnection conn = null;\n    OutputStreamWriter out = null;\n    BufferedReader in = null;\n    String result = null;\n\n    try {\n        URL dataUrl = new URL(url);\n        conn = (HttpsURLConnection) dataUrl.openConnection();\n        conn.setRequestMethod(\"POST\");\n        conn.setRequestProperty(\"Content-Type\", \"application\/json\");\n        conn.setDoOutput(true);\n        conn.setDoInput(true);\n        conn.setUseCaches(false);\n\n        out = new OutputStreamWriter(conn.getOutputStream(), \"UTF-8\");\n        out.write(postData);\n        out.flush();\n\n        in = new BufferedReader(new InputStreamReader(conn.getInputStream()));\n        StringBuffer buffer = new StringBuffer();\n        String line;\n        while ((line = in.readLine()) != null) {\n            buffer.append(line);\n        }\n        result = buffer.toString();\n    } catch (Exception ex) {\n        ex.printStackTrace();\n    } finally {\n        try {\n            if (out != null) {\n                out.close();\n            }\n            if (in != null) {\n                in.close();\n            }\n            if (conn != null) {\n                conn.disconnect();\n            }\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n\n    return result;\n}\n\n\/\/ \u8c03\u7528\u65b9\u6cd5\nOrderInfo orderInfo = new OrderInfo();\n\/\/ ... \u8bbe\u7f6e\u5404\u79cd\u6570\u636e\nString url = \"https:\/\/example.com\/api\/endpoint\";\nString postData = GsonUtils.convertToJson(orderInfo);\nString result = sendPost(url, postData);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff0cgsonutils \u662f\u4e00\u4e2a\u7528\u4e8e json \u8f6c\u6362\u7684\u5de5\u5177\u7c7b\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u884c\u5b9e\u73b0\u6216\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u3002<\/p>\n<p>\u901a\u8fc7\u5c01\u88c5\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e post \u8bf7\u6c42\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u8fde\u63a5\u5efa\u7acb\u7684\u8fc7\u7a0b\uff0c\u5e76\u907f\u514d\u7a7a\u6307\u9488\u5f02\u5e38\u7684\u53d1\u751f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fHTTPS \u63a5\u53e3\u53d1\u9001\u6570\u636e\u65f6\u51fa\u73b0\u7a7a\u6307\u9488\u5f02\u5e38\uff0c\u5982\u4f55\u89e3\u51b3\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>post \u5411 https \u63a5\u53e3\u53d1\u9001\u6570\u636e\uff0c\u90e8\u7f72\u5230\u670d\u52a1\u5668\u540e\u8fd0\u884c\u4e00\u6bb5\u65f6\u95f4\u540e\u62a5\u7a7a\u6307\u9488\u7684\u89e3\u51b3\u65b9\u6cd5 \u95ee\u9898\u4e2d\u7ed9\u51fa\u7684\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c\u5f02\u5e38\u51fa\u73b0\u5728\u521b\u5efa outputstreamwriter \u5bf9\u8c61\u65f6\uff0c\u5373\uff1a outputstreamwriter out = new outputstreamwriter(conn.getoutputstream(), &#8220;utf-8&#8221;); \u767b\u5f55\u540e\u590d\u5236 \u6b64\u884c\u4ee3\u7801\u4e2d\uff0cconn \u4e3a\u4e00\u4e2a httpsurlconnection \u5bf9\u8c61\uff0c\u8c03\u7528\u5176 getoutputstream() \u65b9\u6cd5\u65f6\u51fa\u73b0\u4e86\u7a7a\u6307\u9488\u5f02\u5e38\u3002 \u539f\u56e0\uff1a \u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u51fa\u73b0\u8fd9\u79cd\u60c5\u51b5\u53ef\u80fd\u662f\u7531\u4e8e\u4ee5\u4e0b\u539f\u56e0\uff1a conn \u5bf9\u8c61\u672a\u6b63\u786e\u5efa\u7acb\u8fde\u63a5\u3002 \u670d\u52a1\u5668\u7aef\u53d1\u751f\u5f02\u5e38\u5bfc\u81f4\u65e0\u6cd5\u83b7\u53d6\u8f93\u51fa\u6d41\u3002 \u89e3\u51b3\u65b9\u6cd5\uff1a \u5efa\u8bae\u4f7f\u7528\u4e00\u4e2a\u5355\u72ec\u7684\u65b9\u6cd5\u4e13\u95e8\u7528\u4e8e\u53d1\u9001 post \u8bf7\u6c42\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u5199\u6b7b url \u548c\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4fee\u6539\u540e\u7684\u4ee3\u7801\u793a\u4f8b\uff1a public static String sendPost(String url, String postData) { HttpsURLConnection conn = null; OutputStreamWriter out = null; BufferedReader in = null; String result = null; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-35788","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35788","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=35788"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35788\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}