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

Sending a file to an URL

34 views
Skip to first unread message

David Scemama

unread,
Mar 16, 2004, 6:20:47 AM3/16/04
to
Hi,

I have developed a .NET application that sends an XML file to an URL and
receives an XML file as a result.
Here is the very simple code in vb .NET:

Dim myWebClient As New WebClient
Dim responseArray As Byte() =
myWebClient.UploadFile("http:\\myserver\mypage.php", "myfile.xml")

This works perfectly. But now, I need to write the same piece of code in
C++, without .NET. I've tried to use winhttp without success.
Here is the code I have:

hSession = WinHttpOpen( L"Test/1.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0);

if (hSession)
hConnect = WinHttpConnect( hSession,
L"http:\\myserver\mypage.php",
INTERNET_DEFAULT_HTTP_PORT, 0);

The first error apears here, with an invalid URL error. it only accepts the
command if I use "myserver"

Even then:
if (hConnect)
hRequest = WinHttpOpenRequest( hConnect, L"POST",
L"myfile.xml",
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
if (hRequest)
bResults = WinHttpSendRequest( hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS, 0,
WINHTTP_NO_REQUEST_DATA, 0,
0, 0);

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

Where do I get the answer ?

Can someone help me on that matter or tell me in what direction I have to
look ?

Thanks a lot
David


Biao Wang [MSFT]

unread,
Mar 17, 2004, 3:11:09 AM3/17/04
to
Hi David,

You are using the WinHttp Win32 API. To use this API to upload data, you would first need to read "myfile.xml" into a memory buffer and then supply the buffer pointer and length to WinHttpSendRequest. After the request has been sent, you would call WinHttpReceiveResponse() to read the HTTP response(); once the response is available, you would call WinHttpQueryHeaders to retrieve the status code of the response. If you receive a 200 OK status code, then you would call WinHttpReadData to read the response data, if any. An overview of the API is here

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winhttp/http/choosing_a_winhttp_interface.asp

As you can see the Win32 API is more involved to use then the .NET API. But there is something in the middle that's simpler to code and can be used in C++, and that is the WinHttp IWinHttpRequest COM API.

To use the COM API you would first read your xml file into a IStream object, then you call Open() and Send() followed by get_ResponseText(); You can find sample code here

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winhttp/http/iwinhttprequest_interface.asp

To find out the pros & cons of these two WinHttp APIs, visit http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winhttp/http/choosing_a_winhttp_interface.asp

Hope this helps.

Thanks,
Biao.W. [MSFT]

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

----- David Scemama wrote: -----

David Scemama

unread,
Mar 18, 2004, 5:26:58 AM3/18/04
to
Thanks Biao,

This helps a lot

David

"Biao Wang [MSFT]" <anon...@discussions.microsoft.com> wrote in message
news:FD05C95C-0CD9-4385...@microsoft.com...

0 new messages