kk33 0 Newbie Poster

Hi, I got a problem in my winttp code which is connecting to the URL successfully but not able to POST the data to the the server, here is my main part of the code

hSession = WinHttpOpen(L"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12", 
                                    WINHTTP_ACCESS_TYPE_NO_PROXY,
                                    WINHTTP_NO_PROXY_NAME, 
                                    WINHTTP_NO_PROXY_BYPASS, WINHTTP_FLAG_ASYNC);

if (hSession)
if (!WinHttpSetTimeouts( hSession, 50000, 50000, 50000,50000))
            printf( "Error %u in WinHttpSetTimeouts.\n", GetLastError());




wchar_t parameters[] = L"user=zzzz&password=xxx";

wchar_t objname[] = L"/profile/login";

DWORD dwLen = wcslen(parameters) * 2 ;



if (hSession)
        hConnect = WinHttpConnect( hSession,L"www.website.com",
                                   INTERNET_DEFAULT_HTTPS_PORT, 0);

if (hConnect)
        hRequest = WinHttpOpenRequest( hConnect, L"POST", objname,
                                       NULL, NULL, 
                                       WINHTTP_DEFAULT_ACCEPT_TYPES, 
                                       WINHTTP_FLAG_SECURE);
 if (hRequest)
        bResults = WinHttpSendRequest( hRequest,
                                       WINHTTP_NO_ADDITIONAL_HEADERS,
                                       0, parameters, dwLen, 
                                       dwLen, 0);


 if (bResults)
        bResults = WinHttpReceiveResponse( hRequest, NULL);

After running the program the result shows that I have not put username and password in the username password field, but the POST data is correct, so I assume "WinHttpSendRequest" is failing to send the post data :( , also I am newbie in winhttp coding , so maybe I am punting the POST data to send in the wrong place? please help..


Thanks in advance

Dani AI

Generated

Three likely causes (seen a lot in WinHTTP posts): the request body is being sent as UTF-16 raw bytes instead of the byte encoding the server expects, the request is missing the Content-Type/Content-Length headers, or the session was opened in asynchronous mode but the code treats it like synchronous I/O. For those three are the places to check first.

Fix checklist

  • Send the body as bytes (ASCII or UTF-8) rather than a wchar_t buffer. Convert wide strings with WideCharToMultiByte so the byte count you pass is the actual number of bytes the server expects.
  • Add a Content-Type header (for form posts: application/x-www-form-urlencoded) and pass the exact byte length as dwOptionalLength (or use WinHttpWriteData after WinHttpSendRequest).
  • If you opened the session with WINHTTP_FLAG_ASYNC, either remove that flag (use synchronous calls) or register a status callback with WinHttpSetStatusCallback and wait for the appropriate notifications before calling WinHttpReceiveResponse. Asynchronous sessions require handling the callback events; calling WinHttpReceiveResponse immediately will not behave like a synchronous send/receive.
  • Always check the return values and call GetLastError() on failure. After WinHttpReceiveResponse, call WinHttpQueryHeaders to check the HTTP status code.

Minimal example (concept)

std::string body = "user=zzzz&password=xxx"; // ASCII/UTF-8 bytes
std::wstring headers = L"Content-Type: application/x-www-form-urlencoded\r\n";

BOOL ok = WinHttpSendRequest(hRequest,
    headers.c_str(), (DWORD)-1,
    (LPVOID)body.c_str(), (DWORD)body.size(), (DWORD)body.size(), 0);

See the WinHttpSendRequest documentation for header/body handling and the WideCharToMultiByte doc for conversions: WinHttpSendRequest and WideCharToMultiByte.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.