简体   繁体   English

为什么从.NET中删除了HttpWebRequest和HttpWebResponse的构造函数?

[英]Why were constructors for HttpWebRequest and HttpWebResponse removed from .NET?

Perhaps it doesn't make sense for product code to instantiate these types but the deprecation of the constuctors has made unit testing clients which use the Http implementations of WebRequest/Response far more trouble than it should be. 产品代码实例化这些类型也许没有意义,但是构造函数的弃用使使用WebRequest/ResponseHttp实现的单元测试客户端的麻烦远远超过了应有的程度。 I see no value in taking the constructors away and obvious value in having them, so what is the technical reason for their deprecation? 我认为将构造函数撤走没有任何价值,而让它们具有明显的价值,那么弃用它们的技术原因是什么呢?

Hmm, why exactly do you need the constructor of HttpWebRequest ? 嗯,为什么您确实需要HttpWebRequest的构造函数? I can't see any preferences of it comparing with WebRequest.Create , which is being suggested as preffered way to create such requests in all the versions from the beginning. WebRequest.Create相比,我看不到它的任何首选项,建议将其作为从一开始就在所有版本中创建此类请求的首选方法。

This method is returning the HttpWebRequest as WebRequest , but you still can cast it to the direct type for your needs. 此方法将HttpWebRequest作为WebRequest返回,但是您仍然可以根据需要将其WebRequest转换为直接类型。 If you'll provide some other protocols, as file:// or ftp:// , it will return corresponding types. 如果您提供其他协议,例如file://ftp:// ,它将返回相应的类型。

I think that this Obsolete list is a part of standardizing the .NET Core for all the platforms it'll be used, and it'll be much more simple to maintain the WebRequest creation in one single point of the code. 我认为这个过时列表是将所有使用的平台标准化.NET Core的一部分,并且在代码的单个位置维护WebRequest创建将更加简单。

Update : 更新
I think you've saw this article about deriving the WebRequest class , so I'll just write here another pack of my opinion: 我认为您已经看过有关派生WebRequest类的文章,因此,我将在这里写另一组意见:

When you are dering from the WebRequest , you need two points: WebRequest出发时,需要注意两点:

As the MSDN says: 正如MSDN所说:

The HttpWebRequest class is registered to service requests for HTTP and HTTPS schemes by default. 默认情况下, HttpWebRequest类已注册为服务于HTTP和HTTPS方案的请求。 Attempts to register a different WebRequest descendant for these schemes will fail. 尝试为这些方案注册其他WebRequest后代的尝试将失败。

So the Microsoft wants to control the http and https oriented classes and don't want anybody to replace this functionality with their own. 因此,Microsoft希望控制面向httphttps的类,而不希望任何人用自己的功能替换此功能。 I think that this could be done because of some security concerns. 我认为可以这样做是出于安全考虑。 Sad but true. 悲伤但真实。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM