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
原创粉丝点击