cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: how to send POST request when follow location

From: xinyi huang <huangxinyi.cn_at_gmail.com>
Date: Wed, 13 Jul 2011 22:06:14 +0800

sorry to bother, but I meet another problem now.
when I don't give CURLOPT_WRITEFUNCTION and CURLOPT_WRITEDATA, the code
works fine, successfully locate to new url.
But when I give CURLOPT_WRITEFUNCTION and CURLOPT_WRITEDATA, virtual studio
accurs to exception.
I just don't know why,thanks al ot.

here is the code:

..........................write funtion...............................

size_t writeCallBack(void *data, size_t size, size_t nmemb, void *stream)
{
    return fwrite(data, size, nmemb, (FILE *)stream);
}

..........................................................................

contentFile = "Content.tmp";
    FILE *fContent = fopen(contentFile.c_str(), "w+");
    if (NULL == fContent)
    {
        return ERR;
    }

    CURL * curl = curl_easy_init();
    if (curl == NULL)
    {
        return ERR;
    }

    char error[2048] = {0};
    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    curl_easy_setopt(curl, CURLOPT_URL, curUrl.c_str());
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); //automatolly jump
   // curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 255);
   /curl_easy_setopt(curl, CURLOPT_WRITEDATA, fContent);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallBack);
    //curl_easy_setopt(curl, CURLOPT_HEADER, 1);
   // curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postField.c_str());
    curl_easy_setopt(curl, CURLOPT_POST, 5);

    curlRet = curl_easy_perform(curl);

    if (CURLE_OK == curlRet)
    {
        std::cout<< "success to perform curl. " << endl;
    }
    else
    {
        std::cout<< "fail to perform curl." <<"curlRet:"<<curlRet<< "err:"
<< error << endl;
    }

     curl_easy_cleanup(curl);

2011/7/13 xinyi huang <huangxinyi.cn_at_gmail.com>

> Thanks very very very much. The CURLOPT_POSTREDIR fix my problem.
> And , I changed the error size to CURL_ERROR_SIZE .
> Again, thank you:)
>
>
> 2011/7/13 Daniel Stenberg <daniel_at_haxx.se>
>
>> On Tue, 12 Jul 2011, xinyi huang wrote:
>>
>> I have send a POST request, and set CURLOPT_FOLLOWLOCATION to be TRUE.
>>> The peer http url will location to a new https url.
>>> Output information shows that curl automatically send a new GET request
>>> to
>>> the new url.
>>>
>>
>> It depends entirely on the HTTP response code the server returns. libcurl
>> is made to (by default) mimic how browsers treat them, which for 301 and 302
>> that it will change from POST to GET - contrary to what RFC2616 says we
>> should.
>>
>> You can alter the behavior for both 301 and 302 by the use of the
>> CURLOPT_POSTREDIR option.
>>
>>
>> char error[2048] = {0};
>>> curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
>>>
>>
>> CURL_ERROR_SIZE might be clever to use for the error buffer as it will
>> never be larger!
>>
>> --
>>
>> / daniel.haxx.se
>> ------------------------------**------------------------------**-------
>> List admin: http://cool.haxx.se/list/**listinfo/curl-library<http://cool.haxx.se/list/listinfo/curl-library>
>> Etiquette: http://curl.haxx.se/mail/**etiquette.html<http://curl.haxx.se/mail/etiquette.html>
>>
>
>

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2011-07-13