libcurl进行https的post参数
libcurl开发库可以在官网下载:
http://curl.haxx.se/download.html
用libcurl进行开发,想实现与https的站点的交互,向https://url.cn/APIList ,发送post值: key1=value1&key2=value2;
主要用到的函数是:
CURL_EXTERN CURLcode curl_easy_setopt(CURL *curl, CURLoption option, ...);
对于https://url.cn,需要属性CURLOPT_URL
curl_easy_setopt(curl, CURLOPT_URL, "https://url.cn/APIList");
现在需要启用post方式
curl_easy_setopt(curl,CURLOPT_POST,1);
将post的键值对内容传入
char *pPost = "email=abc@126.com&password=123456"; curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(pPost));//post内容长度 curl_easy_setopt(curl,CURLOPT_POSTFIELDS,pPost);//写入post区域
后续内容就按正常步骤了。
详细代码:
// Win32Demo.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <Windows.h> #include "curl/curl.h" #include <iostream> using namespace std; //utf 转gbk,部分网站中文乱码需要用到,此时可以无视 string UTF8ToGBK(const std::string& strUTF8) { int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0); unsigned short * wszGBK = new unsigned short[len + 1]; memset(wszGBK, 0, len * 2 + 2); MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUTF8.c_str(), -1, (LPWSTR)wszGBK, len); len = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)wszGBK, -1, NULL, 0, NULL, NULL); char *szGBK = new char[len + 1]; memset(szGBK, 0, len + 1); WideCharToMultiByte(CP_ACP,0,(LPWSTR)wszGBK, -1, szGBK, len, NULL, NULL); //strUTF8 = szGBK; std::string strTemp(szGBK); delete[]szGBK; delete[]wszGBK; return strTemp; } int _tmain(int argc, _TCHAR* argv[]) { //https CURL *curl; CURLcode res; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, "https://url.cn/APIList"); curl_easy_setopt(curl,CURLOPT_POST,1); char *pPost = "login_email=abc@126.com&password=123456"; curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(pPost)); curl_easy_setopt(curl,CURLOPT_POSTFIELDS,pPost); //跳过对ca的检查,简单 #define SKIP_PEER_VERIFICATION #ifdef SKIP_PEER_VERIFICATION /* * If you want to connect to a site who isn"t using a certificate that is * signed by one of the certs in the CA bundle you have, you can skip the * verification of the server"s certificate. This makes the connection * A LOT LESS SECURE. * * If you have a CA cert for the server stored someplace else than in the * default bundle, then the CURLOPT_CAPATH option might come handy for * you. */ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); #endif //跳过检查 #define SKIP_HOSTNAME_VERIFICATION #ifdef SKIP_HOSTNAME_VERIFICATION /* * If the site you"re connecting to uses a different host name that what * they have mentioned in their server certificate"s commonName (or * subjectAltName) fields, libcurl will refuse to connect. You can skip * this check, but this will make the connection less secure. */ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); #endif /* Perform the request, res will get the return code */ res = curl_easy_perform(curl); /* Check for errors */ if(res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s ", curl_easy_strerror(res)); /* always cleanup */ curl_easy_cleanup(curl); } curl_global_cleanup(); getchar(); return 0; }
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: curl以POST方式请求https协议接口
- 下一篇: CURL HTTPS POST