windows HttpWebRequest和HttpWebResponse的C++接口版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1907131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:35:41  来源:igfitidea点击:

C++ interface version of HttpWebRequest and HttpWebResponse

windowsinternet-explorerhttpwebrequest

提问by Ramanand Bhat

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.

我们想知道如何使用 ATL c++ 项目中的 HttpWebRequest 和 HttpWebResponse .net 框架类是它们为 C++ 中的 webrequest 类公开的任何接口,目前我们无法拥有 ac# 项目,因此我们正在寻找替代接口。

Any help will be greatly appreciated. Ramanand.

任何帮助将不胜感激。拉马南。

回答by feroze

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.

1) 将托管的 HttpWebRequest 代码写入 C# 文件,并将其编译为 DLL。使用 RegAsm.exe 将其注册为 COM 对象。使用来自 C/C++ 应用程序的 COM 对象。

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

2)正如迈克尔在上面建议的那样,使用托管 C++ 编写代码,并与 C/C++ 代码的其他部分互操作/接口。

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

3)不要使用托管代码!使用特定于平台的库 - 例如,来自 Microsoft 的WinHTTP已经过良好测试,并且支持客户端和服务器端操作。您也可以使用Internet Explorer使用的Wininet,但不建议在中间层场景中使用。

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.

因此,除非您真的需要 Wininet/WinHTTP 上不可用的 System.Net 托管代码命名空间提供的东西,否则我不会选择托管代码。托管代码将带来内存和 CPU 开销,如果您所做的只是下载网页,则实际上不需要这些开销。

回答by Raymond

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

请参考这篇文章:How do you make a HTTP request with C++?

libcurlis recommended in many posts.

许多帖子都推荐使用libcurl

回答by Michael A. McCloskey

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

您必须使用 C++/CLI。代码片段可能看起来像这样。

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