Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

winhttp post request

2,152 views
Skip to first unread message

gigino

unread,
Nov 4, 2002, 4:23:58 PM11/4/02
to
can someone please show me a sample code of a post request
in c++ with winhttp? I've followed all instructions but i
wasn't able to make it work...

Stephen Sulzer (Microsoft)

unread,
Nov 7, 2002, 6:51:53 PM11/7/02
to
Hello,

Are you using the WinHTTP Win32 API or the WinHttpRequest COM component?

Can you post an example of your code please?


Regards,

Stephen Sulzer
Microsoft Corporation


This posting is provided "AS IS" with no warranties, and confers no rights.

"gigino" <invisibil...@yahoo.it> wrote in message
news:5ffb01c28448$7cebb8d0$36ef2ecf@tkmsftngxa12...

mike martin

unread,
Nov 8, 2002, 1:32:26 PM11/8/02
to
Hey,
I'm having a POST problem as well, if you dont mind helping... I'm in
the process of converting some code that does GETs and POSTs using
wininnet to use winhttp. GET is working ok. POST seems to work, but
the page I'm posting to never actually receives any of the data. FOr
example, the asp page I am posting to consists of just:

Dim bd

bd = request.form("bd")

response.write "body=" & bd & "ENDBD"


But bd is always empty. Here is my relevant code:

CString strFormData = "BD=testbody";
LPCWSTR pwszHeader = L"Content-Type:
application/x-www-form-urlencoded";

hRequest = WinHttpOpenRequest( hConnect, L"POST",
strObject.AllocSysString(),
L"HTTP/1.1", WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES, 0);

bResults = WinHttpSendRequest( hRequest,
pwszHeader,
-1L,
strFormData.AllocSysString(),
strFormData.GetLength(),
strFormData.GetLength(),
0);


I've played around with the different "length" parameters in
SendRequest with no luck and I've also tried calling
WinHttpAddRequestHeaders to set the headers, which also didnt help.
I'm sure it's something real simple that I am missing here.

Thanks for any help,
mike martin

"Stephen Sulzer \(Microsoft\)" <ssu...@online.microsoft.com> wrote in message news:<OQtF2hrhCHA.1392@tkmsftngp12>...

Stephen Sulzer [Microsoft]

unread,
Nov 9, 2002, 12:33:52 AM11/9/02
to
Hi Mike,

Does strFormData.AllocSysString() return a wide-character Unicode string, or
a single-byte character ANSI string? If it is returning a wide-character
string, then that is likely causing a problem. The WinHttpSendRequest API
expects a pointer to BYTE or single-character string data, not
wide-character string data.

Regards,

Stephen Sulzer
Microsoft Corporation


This posting is provided "AS IS" with no warranties, and confers no rights.

"mike martin" <mma...@notifycorp.com> wrote in message
news:de469d9e.02110...@posting.google.com...

mike martin

unread,
Nov 11, 2002, 8:38:10 AM11/11/02
to
Thanks. That was the problem.


"Stephen Sulzer [Microsoft]" <ssu...@online.microsoft.com> wrote in message news:<#xoHlF7hCHA.2156@tkmsftngp12>...

Gigino

unread,
Nov 11, 2002, 12:01:35 PM11/11/02
to
"Stephen Sulzer \(Microsoft\)" <ssu...@online.microsoft.com> wrote in message news:<OQtF2hrhCHA.1392@tkmsftngp12>...


I'm using the Winhttp win32 API; the segment of code i wrote to post
data to a server is the following; i followed an example of winhttp
help in which there was only the download of an html page and i've
made the changes i thought i needed to post data, but it doesn't
work...
"CS=friuli" is the string i want to post, the page is
www.myservarname.it/rs/find.asp; the error i get is 12002 (timeout),
but the problem seems to be the post... if i only read the page
without posting data, there is no problem.
Thank you for any help
Gigino

DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;


// obtain a session handle.
hSession = WinHttpOpen( L"WinHTTP Example/1.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0);

// Specify an HTTP server.
if (hSession)
hConnect = WinHttpConnect( hSession, L"www.myservarname.it",
INTERNET_DEFAULT_HTTPS_PORT, 0);

// Create an HTTP request handle.
if (hConnect)


hRequest = WinHttpOpenRequest( hConnect,
L"POST",

L"/rs/find.asp", NULL,
WINHTTP_NO_REFERER,

WINHTTP_DEFAULT_ACCEPT_TYPES,
0);

bResults=WinHttpAddRequestHeaders(hRequest,
L"Content-Type:
application/x-www-form-urlencoded\r\n\r\n",
-1L, WINHTTP_ADDREQ_FLAG_ADD);

// Send a request.
if (hRequest)
bResults =WinHttpSendRequest(hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS , 0,
"CS=friuli", 18, 18,
0);


// End the request.
if (bResults)
bResults = WinHttpReceiveResponse( hRequest, NULL);

// Keep checking for data until there is nothing left.
if (bResults)
do
{

// Check for available data.
dwSize = 0;
if (!WinHttpQueryDataAvailable( hRequest, &dwSize))
printf("Error %u in
WinHttpQueryDataAvailable.\n",GetLastError());

// Allocate space for the buffer.
pszOutBuffer = new char[dwSize+1];
if (!pszOutBuffer)
{
printf("Out of memory\n");
dwSize=0;
}
else
{
// Read the data.
ZeroMemory(pszOutBuffer, dwSize+1);

if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer,
dwSize, &dwDownloaded))
printf("Error %u in WinHttpReadData.\n",
GetLastError());
else
printf("%s", pszOutBuffer);

// Free the memory allocated to the buffer.
delete [] pszOutBuffer;
}

} while (dwSize>0);


// Report any errors.
if (!bResults)
printf("Error %d has occurred.\n",GetLastError());

// Close any open handles.
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);

return 1;

Stephen Sulzer [Microsoft]

unread,
Nov 11, 2002, 4:17:39 PM11/11/02
to

Hello Gigino,

I think the problem is the WinHttpSendRequest call:

> // Send a request.
> if (hRequest)
> bResults =WinHttpSendRequest(hRequest,
> WINHTTP_NO_ADDITIONAL_HEADERS , 0,
> "CS=friuli", 18, 18,
> 0);

This code is posting a 9 byte string, but specifies that the length is 18
bytes. It should use 9 instead, as the payload is a 9-byte ANSI string, not
a wide-character Unicode string. Sending the string as single-byte ANSI is
the correct thing to do. The dwOptionalLength and dwTotalLength parameters
must specify the length of the data in bytes.

WinHTTP will use the dwTotalLength parameter to generate the Content-Length
header. So the server is expecting 18 bytes of data, and this would cause
the timeout error.

Also, when calling the WinHttpAddRequestHeaders function, you do not need
the \r\n characters at the end of the string, so the following is
sufficient:

bResults=WinHttpAddRequestHeaders(hRequest,
L"Content-Type: application/x-www-form-urlencoded",
-1L, WINHTTP_ADDREQ_FLAG_ADD);


Hope that helps.

Regards,

Stephen Sulzer
Microsoft Corporation


This posting is provided "AS IS" with no warranties, and confers no rights.

"Gigino" <realth...@yahoo.it> wrote in message
news:18b07ef0.02111...@posting.google.com...

Gigino

unread,
Nov 13, 2002, 4:15:24 AM11/13/02
to
Hi Stephen,
the funcion gets the error in the WinHttpSendRequest call, but i've
also tried to set to 9 and even to 0 the length of the data to see if
this was the problem, but the behaviour is the same.
Another think i don't understand is that i've also tried to modify a
sample from winhttp help (asyncdemo) to post a string and it works!
the differences from the code i shown are mainly these:
* in the WinHttpOpen last parameter is set to WINHTTP_FLAG_ASYNC (that
is it uses the WinHTTP functions asynchronously);
* before the WinHttpAddRequestHeaders call:
// Install the status callback function.
WinHttpSetStatusCallback( rcContext1.hRequest,

(WINHTTP_STATUS_CALLBACK)AsyncCallback,

WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, NULL);*/
* the WinHttpSendRequest is called with the context parameter (the
last one) set to the value that is passed to the callback function
* the page from the server is read inside the callback function

I've copied the calls from this functions changing only the parts
relating to sync/async and the callback function, but i got error 4317
(ERROR_INVALID_OPERATION) in the WinHttpReceiveResponse call (the
WinHttpSendRequest returns 1...)
I really don't understand why this one works and the other example
doesn't...
thanks
Gigino


"Stephen Sulzer [Microsoft]" <ssu...@online.microsoft.com> wrote in message news:<eOcGReciCHA.2380@tkmsftngp09>...

Gigino

unread,
Nov 13, 2002, 6:04:33 AM11/13/02
to
Hello Stephen!
now it works! I think that the only problem was the
INTERNET_DEFAULT_HTTPS_PORT option in the WinHttpConnect, instead of
INTERNET_DEFAULT_HTTP_PORT...
i've posted another message, please ignore it...
thank you for your help...
Gigino

"Stephen Sulzer [Microsoft]" <ssu...@online.microsoft.com> wrote in message news:<eOcGReciCHA.2380@tkmsftngp09>...

Safi Ullah Warraich

unread,
Apr 27, 2021, 7:49:30 AM4/27/21
to
Hi Stephen,
i am having an issue with the POST API my API seems to work ok as when i send data from postman it works absolutely fine but when i send the data from c++ it doesn't post any thing as there is a key whose value is quite long
here is my code below if you can help me
wstrURL+=L"Id=";
wstrURL += _id;//after this concatinate id provided
wstrURL+=L"&Email=";
wstrURL+=Email;//add email here
wstrURL+=L"&Public_Key=";
wstrURL+=_publicKey;//add public key
wstrURL += L"&Private_Key=";
wstrURL +=_privateKey; //add private key
//wstrURL += L"Content-Type: application/x-www-form-urlencoded\r\n";
WinHttpClient* pHttpClient = NULL;
pHttpClient = new WinHttpClient(wstrURL);
if (pHttpClient == NULL)
{
return;
}
LPCWSTR additionalHeaders = L"Content-Type: application/x-www-form-urlencoded\r\n";
//pHttpClient->SetAdditionalRequestHeaders(additionalHeaders);
pHttpClient->SendHttpRequest(L"POST",additionalHeaders);
0 new messages