{"id":1454,"date":"2018-06-21T10:22:04","date_gmt":"2018-06-21T02:22:04","guid":{"rendered":"http:\/\/ykyi.net\/?p=1454"},"modified":"2018-06-21T10:22:04","modified_gmt":"2018-06-21T02:22:04","slug":"c-c%e7%94%a8%e4%b8%87%e6%81%b6%e7%9a%84libcurl%e9%80%8f%e8%bf%87https%e5%8f%91postjson%e8%af%b7%e6%b1%82","status":"publish","type":"post","link":"https:\/\/ykyi.net\/?p=1454","title":{"rendered":"C\/C++\u7528\u4e07\u6076\u7684libcurl\u900f\u8fc7HTTPS\u53d1POST+JSON\u8bf7\u6c42"},"content":{"rendered":"<p>\u76f8\u6bd4python, nodejs\u8fd9\u6837\u7684\u811a\u672c\u5f00\u53d1\u8bed\u8a00\u5df2\u6709\u975e\u5e38\u597d\u7528\u7684RESTful\u5f00\u53d1\u5e93\uff0c\u5f88\u591aC\/C++\u7a0b\u5e8f\u5728\u8c03\u7528RESTful\u63a5\u53e3\u65f6\uff0c\u8fd8\u5728\u4f7f\u7528\u975e\u5e38\u96be\u4f7f\u7528\u7684libcurl\u7eafC\u63a5\u53e3\u3002\u4e0b\u9762\u662f\u7528libcurl\u900f\u8fc7HTTPS\u53d1POST\u8bf7\u6c42\u7684\u4ee3\u7801\u4ee3\u6bb5\uff0chttp body\u662fjson\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre><code class=\"\">    #include &lt;curl\/curl.h&gt;\n\n    CURL* curl;\n    struct curl_slist* headers;\n    curl = curl_easy_init();\n    headers_ = curl_slist_append(headers, \"Content-Type: application\/json\");\n    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);\n    curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, \"PEM\");\n    curl_easy_setopt(curl, CURLOPT_SSLCERT, cert_file_path.c_str());\n    curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, \"PEM\");\n    curl_easy_setopt(curl, CURLOPT_SSLKEY, key_file_path.c_str());\n    curl_easy_setopt(curl, CURLOPT_CAINFO, ca_file_path.c_str());\n    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);\n    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, -1L);\n\n    \/\/ jv_text\u5b58\u4e86json\u5b57\u7b26\u4e32\n    curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, jv_text.c_str());\n    curl_easy_setopt(curl_, CURLOPT_URL, path.c_str());\n\n    CURLcode res = curl_easy_perform(curl_);\n    if (res != CURLE_OK)   \/\/ \u6ca1\u6709\u6210\u529f\n    {\n        LG_ERR(\"curl_easy_perform() failed: %s\", curl_easy_strerror(res));\n    }\n    else  \/\/ \u6210\u529f\u4e86\n    {\n    }\n    curl_easy_cleanup(curl);\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u76f8\u6bd4python, nodejs\u8fd9\u6837\u7684\u811a\u672c\u5f00\u53d1\u8bed\u8a00\u5df2\u6709\u975e\u5e38\u597d\u7528\u7684RESTful\u5f00\u53d1\u5e93\uff0c\u5f88\u591aC\/C++\u7a0b\u5e8f\u5728\u8c03\u7528RESTful\u63a5\u53e3\u65f6\uff0c\u8fd8\u5728\u4f7f\u7528\u975e\u5e38\u96be\u4f7f\u7528\u7684libcurl\u7eafC\u63a5\u53e3\u3002\u4e0b\u9762\u662f\u7528libcurl\u900f\u8fc7HTTPS\u53d1POST\u8bf7\u6c42\u7684\u4ee3\u7801\u4ee3\u6bb5\uff0chttp body\u662fjson\u5b57\u7b26\u4e32\uff1a #include &lt;curl\/curl.h&gt; CURL* curl; struct curl_slist* headers; curl = curl_easy_init(); headers_ = curl_slist_append(headers, &#8220;Content-Type: application\/json&#8221;); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, &#8220;PEM&#8221;); curl_easy_setopt(curl, CURLOPT_SSLCERT, cert_file_path.c_str()); curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, &#8220;PEM&#8221;); curl_easy_setopt(curl, CURLOPT_SSLKEY, key_file_path.c_str()); curl_easy_setopt(curl, CURLOPT_CAINFO, ca_file_path.c_str()); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, -1L); \/\/ jv_text\u5b58\u4e86json\u5b57\u7b26\u4e32 curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, jv_text.c_str()); curl_easy_setopt(curl_, CURLOPT_URL, path.c_str()); CURLcode res = curl_easy_perform(curl_); if &hellip; <a href=\"https:\/\/ykyi.net\/?p=1454\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;C\/C++\u7528\u4e07\u6076\u7684libcurl\u900f\u8fc7HTTPS\u53d1POST+JSON\u8bf7\u6c42&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-1454","post","type-post","status-publish","format-standard","hentry","category-tech_articles"],"_links":{"self":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts\/1454","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1454"}],"version-history":[{"count":0,"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts\/1454\/revisions"}],"wp:attachment":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}