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\n",
              curl_easy_strerror(res));
 
    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }
 
  curl_global_cleanup();

	getchar();
	return 0;
}



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值