curl-library
how do i post json to a https ?
Date: Sat, 10 Jan 2015 00:03:09 +0800 (CST)
hello everyone
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
#include <stdio.h>
#include <curl/curl.h>
#include "slhttpclient.h"
#include "cJSON.h"
struct WriteThis {
const char *readptr
int sizeleft
}
static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
{
struct WriteThis *pooh = (struct WriteThis *)userp
if(size*nmemb sizeleft) {
*(char *)ptr = pooh->readptr[0] /* copy one single byte */
pooh->readptr++ /* advance pointer */
pooh->sizeleft-- /* less data left */
return 1 /* we return 1 byte at a time! */
}
return 0 /* no more data left to deliver */
}
int __stdcall sendmydata(const char * user,const char * pass,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)
{
CURL *curl
CURLcode res
struct WriteThis pooh
//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)
pooh.readptr = out
pooh.sizeleft = strlen(out)
curl = curl_easy_init()
if(curl) {
/* First set the URL that is about to receive our POST. This URL can
just as well be a https:// URL if that is what should receive the
data. */
curl_easy_setopt(curl, CURLOPT_URL, "https://www.myexample.com")
/* Perform the request, res will get the return code */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, out)
res = curl_easy_perform(curl)
free(out)
/* always cleanup */
curl_easy_cleanup(curl)
}
return 0
}
ps:i have complied the static library in VC++ project with openssl,the libcurl is support SSL
best regards
ken
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2015-01-09