Search code examples
c++winapiwininet

POST request to a friendly URL fails


I'm trying to post an HTTP request to an HTML form that is accessible through the friendly URL address.
But when I post it, library returns error code "bad URL provided".

HINTERNET hRequest = HttpOpenRequest(hConn, L"POST", L"newform",
                     NULL, L"http://www.example.com/add/newform/",
                     rgpszAcceptTypes, NULL, NULL);
DWORD error_code   = GetLastError();
bool hResult       = HttpSendRequestW(hRequest,NULL,NULL,NULL,NULL);
error_code         = GetLastError(); //12005 - bad URL provided  

What am I doing wrong, why does my request fails?


Solution

  • The server name (www.site.com in your above example) must be specified in the call to InternetConnect, as must the protocol (INTERNET_SERVICE_HTTP).

    In the call to HttpOpenRequest you only give the name of the "object", which in this case is "/add/newform/".

    For example (added for more clarity):

    HINTERNET hConn = InternetConnect(hInternet, L"www.site.com", INTERNET_DEFAULT_HTTP_PORT,
        NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
    HINTERNET hRequest = HttpOpenRequest(hConn, L"POST", L"/add/newform", NULL, NULL,
        rgpszAcceptTypes, NULL, NULL);