C++ interface version of HttpWebRequest and HttpWebResponse
We are wondering how to use HttpWebRequest and HttpWebResponse .net framework Class in ATL c++ project is their 开发者_C百科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.
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.
please refer to this post: How do you make a HTTP request with C++?
libcurl is recommended in many posts.
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();
精彩评论