Archived Forum Post

Index of archived forum posts

Question:

HTTP QuickGetStr fails with 406 Not Acceptable

Jul 22 '14 at 11:09

For this particular URL ("http://www.sinyi.com.tw/"), the call to quickGetStr fails because the server returns a "406 Not Acceptable" response code.

    const char url = "http://www.sinyi.com.tw/";
    const char html = http.quickGetStr(url);
    if (!html)
    {
    printf("qa_httpGet failed.n");
    printf("%sn",http.lastErrorText());
    }


Answer

For some reason, this web server responds with a 406 Not Acceptable to the 1st GET request, but included in the response header is a Set-Cookie response header. The solution is to have your app automatically save/send cookies. Issue the 1st GET to get the 406 response, then re-issue the GET a 2nd time, which automatically sends the cookie with the GET request, and this results in a 200 OK.

    const char *url = "http://www.sinyi.com.tw/";

http.put_SendCookies(true);
http.put_SaveCookies(true);
http.put_CookieDir("memory");

const char *html = http.quickGetStr(url);
if (!html)
{
printf("qa_httpGet failed.\n");
printf("%s\n",http.lastErrorText());
}
// Send the request again, but this time the cached cookie(s) from the 1st response 
// will be included.
html = http.quickGetStr(url);
if (!html)
{
printf("qa_httpGet failed.\n");
printf("%s\n",http.lastErrorText());
return false;
}
printf("%s\n",http.lastErrorText());
printf("qa_httpGet success.\n");