views:

506

answers:

3

Hi All,

We are wondering how to use HttpWebRequest and HttpWebResponse .net framework Class in ATL c++ project is their any interface exposed for webrequest class in C++, currently we cannot have a c# project so we are looking for alternative interface.

Any help will be greatly appreciated. Ramanand.

+2  A: 

please refer to this post: http://stackoverflow.com/questions/1011339/how-do-you-make-a-http-request-with-c

libcurl is recommended in many posts.

Raymond
+1  A: 

You have to use C++/CLI. A code snippet might look something like this.

// Open a connection
System::Net::HttpWebRequest ^_HttpWebRequest = safe_cast<System::Net::HttpWebRequest^>(System::Net::HttpWebRequest::Create(_URL));

_HttpWebRequest->AllowWriteStreamBuffering = true;

// You can specify additional header values:
_HttpWebRequest->UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
_HttpWebRequest->Referer = "http://www.google.com/";

// set timeout for 20 seconds (Optional)
_HttpWebRequest->Timeout = 20000;

// Request response:
System::Net::WebResponse ^_WebResponse = _HttpWebRequest->GetResponse();

// Open data stream:
System::IO::Stream ^_WebStream = _WebResponse->GetResponseStream();

// Do something with stream
_tmpImage = Image::FromStream(_WebStream);

// Cleanup
_WebResponse->Close();
_WebResponse->Close();
Michael McCloskey
+1  A: 

You have the following options:

1) Write your managed HttpWebRequest code into a C# file, and compile it as a DLL. Use RegAsm.exe to register it as a COM object. Use the COM object from the C/C++ application.

2) As Michael has suggested above, use Managed C++ to write the code, and interop/interface with other parts of your C/C++ code.

3) Dont use managed code! Use the platform specific libraries - for eg, WinHTTP from Microsoft is well tested, and supported for both client side and server side operations. You can also use Wininet which is what is used by Internet Explorer, however it is not recommended for use in Middle-tier scenarios.

So,unless you really need something that is offered by System.Net managed code namespace that is not available on Wininet/WinHTTP, I would not opt for managed code. Managed code will bring in memory and cpu overhead that is really not needed if all you are doing is downloading web pages.

feroze
Really thanks for the update i will b using WinHttp/Wininet method to download method.
Ramanand Bhat