cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re:Re: how do i post json to a https ?

From: redmond <redmond_at_139.com>
Date: Sat, 10 Jan 2015 12:25:30 +0800 (CST)

with all your recommandation,i have modified my code,please take a look at my code,is it correct?i reference the code from
https://github.com/polyu/Cloud_Game/blob/master/ClientGUI/GUI/httpclient.cpp

i have question,due to my code is use as a dynamic link library,however the static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid) funcition is not allow using in dll,is it correct?how do i modify this function that i could use it in dll?

//mycode is dll
//"httpclient.h"

typedef char * LPCSTR

extern "C"
  {
   __declspec(dllexport) int __stdcall sendmyinv(const char* theurl,const char * user,const char * pass,const char * pCaPath,const int recordcount,const char * prodno,const char * prodadd,const char * batchno,const double* invnum,const double * stdprice,const double * sellprice,const char * clino,const char * invbywho)
  }

//"httpclient.cpp"

#include <stdio.h>
#include <curl/curl.h>
#include "httpclient.h"
#include "cJSON.h"

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)
return nmemb
}

int __stdcall sendmyinv(const char* theurl,const char * user,const char * pass,const char * pCaPath,const int recordcount,const char * prodno,const char * prodadd,const char * batchno,const char* invnum,const char * stdprice,const char * sellprice,const char * clino,const char * invbywho)
{

  CURLcode res
  //serialze to json
  cJSON *root
  root=cJSON_CreateObject()
  cJSON_AddStringToObject(root, "user", user)
  cJSON_AddStringToObject(root, "pass", pass)
  cJSON_AddNumberToObject(root, "recordcount", pass)
  cJSON_AddStringToObject(root, "prodno", prodno)
  cJSON_AddStringToObject(root, "prodadd", prodadd)
  cJSON_AddStringToObject(root, "batchno", batchno)
  cJSON_AddStringToObject(root, "invnum", invnum)
  cJSON_AddStringToObject(root, "stdprice", stdprice)
  cJSON_AddStringToObject(root, "sellprice", sellprice)
  cJSON_AddStringToObject(root, "clino",clino)
  cJSON_AddStringToObject(root, "invbywho",invbywho)

  char *out
  out=cJSON_Print(root)
  std::string strResponse=""
  int thereturncode=0

  CURL* curl = curl_easy_init()
  if(NULL == curl)
  {
    return CURLE_FAILED_INIT
  }
 
    curl_easy_setopt(curl, CURLOPT_URL, theurl)
    curl_easy_setopt(curl, CURLOPT_POST, 1)
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, out)
    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)
   
    if(NULL == pCaPath)
    {
      curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false)
      curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false)
    }
    else
    {
       //default is PEM,in addition support DER
       curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true)
       curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath)
    }
        curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3)
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3)
        res = curl_easy_perform(curl)

        

         if(res != CURLE_OK)
         {
           
           free(out)
           curl_easy_cleanup(curl)
           return res
         }
         else
         {
          
          //json deserialize
           size_t slen=strlen(strResponse.c_str())+1
           char * json = new json[slen]
           json =cJSON_Parse(strResponse.c_str())
           thereturncode=cJSON_GetObjectItem(json, "returncode")->valueint
     
         //

          free(out)
          curl_easy_cleanup(curl)
          return thereturncode
         }
        
  
  
}with with

--------------------------------------------------------------------------------

------------------ 原始邮件 ------------------

 
 发件人: Ray Satiro <raysatiro_at_yahoo.com>
 

 
 发送时间: 2015-01-10 09:44:08
 

 
 收件人: curl-library <curl-library_at_cool.haxx.se>
 

 
 抄送: (无)
 

 
 主题: Re: how do i post json to a https ?
 

 On 1/9/2015 11:03 AM, redmond wrote:
 
though im unfamilar with C/C++,i have wrote the following code with libcurl docs,how do i post the json on https(the ssl website has no cert,just like https://www.google.com,it don't need the cert)?i search google for this,however there is no answer.i tried to write the following code,are these correct?how do i determine the http server respond ok ?in addition,static method is not allow use in dll
 
 
 Typically the SSL backend used by libcurl will have a database of certificates and/or you'll have to load your own to verify your website.
 
 In your code the read function really isn't necessary for what you are doing. As documented in CURLOPT_POST [1] you'll need chunked transfer encoding if you are going to use a read function. I made an example for you without a read function at https://gist.github.com/jay/2a6c54cc6da442489561
 
 
 [1]: http://curl.haxx.se/libcurl/c/CURLOPT_POST.html
 
 

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2015-01-10