curl-library
Re: how do i post json to a https ?
Date: Sat, 10 Jan 2015 03:14:58 -0500
On 1/9/2015 11:25 PM, redmond wrote:
> 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
Before you do anything else please review the libcurl documentation;
there's an overview at [1]. Take a look at some of the examples as well.
Here are some things wrong with that code, probably not all:
Call curl_global_init once at the very beginning of the program rather
than for each object creation. Technically this is fine but the first
call should be before any other threads have started. See [2] for more
on that.
The debug callback will receive data that is not null terminated [3]. If
you can print the data as a string you would only print 'size' number of
characters, eg printf("%.*s", size, data);
CURLOPT_POSTFIELDS does not copy the data [4]. It's bad practice to set
CURLOPT_POSTFIELDS to a c_str because the pointer c_str returns could be
invalidated when the string is modified or destroyed.
The write function must return the amount of bytes handled, in this case
(size * nmemb). Also the number of bytes could be 0. See [5].
> 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?
I'm not sure what you mean by this. Why can't you use the callback in a DLL?
> //mycode is dll
> //"httpclient.h"
I only took a brief look at this. I think you need help that is really
outside of the scope of this mailing list. A few things though:
Review dllexport/import at [6].
Your function's return value is either a curlcode or the value of
"returncode". That's bad practice, it will lead to trouble. I would have
return values specifically for your function and whether it failed or
succeeded. If the caller needs that extra information make it available
through output parameters, eg func( in1, in2, in3, CURLcode *out1,
ServerCode *out2)
then if(!func) caller can work with curlcode or server code if need be.
Actually given your number of input parameters I'd throw all that in a
struct and pass a pointer to it so func( JSONinput *in, CURLcode *out1,
ServerCode *out2) maybe.
Also that deserialize is wrong. If you need help with cJSON this list
can't help you, go to their help forum at [7]. Also check out any C
programming forums, MSDN forums and stackoverflow.
[1]: http://curl.haxx.se/libcurl/c/libcurl-easy.html
[2]: http://curl.haxx.se/libcurl/c/curl_global_init.html
[3]: http://curl.haxx.se/libcurl/c/CURLOPT_DEBUGFUNCTION.html
[4]: http://curl.haxx.se/libcurl/c/CURLOPT_POSTFIELDS.html
[5]: http://curl.haxx.se/libcurl/c/CURLOPT_WRITEFUNCTION.html
[6]: http://msdn.microsoft.com/en-us/library/8fskxacy.aspx
[7]: http://sourceforge.net/p/cjson/discussion/998970/
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2015-01-10