C++ interface version of HttpWebRequest and HttpWebResponse

感情迁移 提交于 2021-01-28 09:59:30

问题


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.


回答1:


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.




回答2:


please refer to this post: How do you make a HTTP request with C++?

libcurl is recommended in many posts.




回答3:


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();


来源:https://stackoverflow.com/questions/1907131/c-interface-version-of-httpwebrequest-and-httpwebresponse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!