curl 实现 url连接 post Json消息
来源:互联网 发布:千牛什么时候出mac版 编辑:程序博客网 时间:2024/06/05 11:55
1..h文件#pragma once
#include <stdio.h>
#include <curl/curl.h>
#include <string>
#pragma comment(lib,"libcurl_imp.lib")
#undef DISABLE_SSH_AGENT
bool PostMessage_Url (const std::string & strUrl, const std::string & strPost, std::string & strResponse, tstring &strError, int &iError);
bool GetMessage_Url (const std::string & strUrl, std::string & strResponse, tstring &strError, int &iError);
void GetErrorInfo (int resId, std::wstring & strError);
static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid) { std::string* str = dynamic_cast<std::string*>((std::string *)lpVoid); if( NULL == str || NULL == buffer ) { return -1; } char* pData = (char*)buffer; str->append(pData, size * nmemb); printf("pData [ %s ] \n", str);return nmemb; }
static int OnDebug(CURL *, curl_infotype itype, char * pData, size_t size, void *) { if(itype == CURLINFO_TEXT) { //printf("[TEXT]%s\n", pData); } else if(itype == CURLINFO_HEADER_IN) { printf("[HEADER_IN]%s\n", pData); } else if(itype == CURLINFO_HEADER_OUT) { printf("[HEADER_OUT]%s\n", pData); } else if(itype == CURLINFO_DATA_IN) { printf("[DATA_IN]%s\n", pData); } else if(itype == CURLINFO_DATA_OUT) { printf("[DATA_OUT]%s\n", pData); } return 0; }
//strUrl url
//strPostRequest post,json消息
//strResponse 返回消息
//strError 返回错误信息
bool CSftp::PostMessage_Url(const std::string & strUrl, const std::string & strPostRequest, std::string & strResponse, tstring &strError, int &iError){bool bError = true;CURLcode res;CURL *curl;struct curl_slist *headers = NULL; // init to NULL is important headers = curl_slist_append( headers, "Accept: application/json"); headers = curl_slist_append( headers, "Content-Type: application/json");headers = curl_slist_append( headers, "charsets: utf-8"); curl = curl_easy_init();curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str()); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); curl_easy_setopt(curl, CURLOPT_POST, 1L); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPostRequest.c_str()); curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3); res = curl_easy_perform(curl); curl_easy_cleanup(curl); if(CURLE_OK != res) {fprintf(stderr, "curl told us %d\n", res);bError = false;GetErrorInfo(res, strError);iError = res;}return bError;}bool CSftp::GetMessage_Url(const std::string & strUrl, std::string & strResponse, tstring &strError, int &iError) { bool bError = true;CURLcode res;CURL *curl;struct curl_slist *headers = NULL; headers = curl_slist_append( headers, "Accept: application/json"); headers = curl_slist_append( headers, "Content-Type: application/json");headers = curl_slist_append( headers, "charsets: utf-8"); curl = curl_easy_init();if(NULL == curl) { return CURLE_FAILED_INIT; } if(m_bDebug) { curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug); } curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str()); curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); res = curl_easy_perform(curl); if(CURLE_OK != res) {fprintf(stderr, "curl told us %d\n", res);bError = false;GetErrorInfo(res, strError);iError = res;}curl_easy_cleanup(curl); return bError; } void CSftp::GetErrorInfo (int resId, std::wstring & strError){switch (resId){case CURLE_COULDNT_CONNECT:strError = _T("couldn't connect");break;case CURLE_LOGIN_DENIED:strError = _T("user, password or similar was not accepted and we failed to login.");break;case CURLE_REMOTE_FILE_NOT_FOUND:strError = _T("remote file not found");break;case CURLE_COULDNT_RESOLVE_HOST:strError = _T("couldn't resolve host");break;default:strError = resId;break;}}
实例下载地址:
http://download.csdn.net/detail/leftstrang/9632741
1 0
- curl 实现 url连接 post Json消息
- curl with post json
- CURL POST json字符串
- curl post json
- curl命令Post Json
- curl post发送json数组
- 使用curl发送 POST JSON
- PHP特性01:cURL实现get和post,在url间做数据交互;
- Curl POST to HTTPS url gives SSLRead() error:curl: (56)
- .net post url获取json
- curl实现GET和POST
- cURL实现Get和Post
- php cUrl 通过 http post json数据
- CURL模拟POST提交JSON数据
- 使用curl post json到webApi
- php curl post raw json参数
- curl perl python post发送json数据
- post json 请求url第三方url
- HDU 5883 The Best Path (一笔画 / 欧拉通路)
- C#中的静态与非静态方法比较
- Java 8新特性
- java实现八大排序
- 关于线程同步和互斥的关系与区别
- curl 实现 url连接 post Json消息
- C语言内存分布
- caffe结构解析
- 单利模式小结
- javascript select控件的使用及左右移动item的逻辑处理
- python:how does subclass call baseclass's __init__()
- Hibernate详解
- mysql数据类型
- Mysql 命令大全