HttpWebRequest 与添加服务引用
有人可以定义使用 Web 服务时 HttpWebRequest 和添加服务引用之间的区别吗?还有什么是最好的方法。
Can somebody please define the difference between HttpWebRequest and Adding service reference when using web services? Also what is the best approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
添加服务引用可以让您受益于调用 Web 服务方法所需的管道代码,就像您只是调用方法一样。它抽象处理请求/响应。
Adding a service reference gives you the benefit of the plumbing code necessary to call the web service methods as if you were just calling methods. It abstracts dealing with the request/response.
我认为,就通过 HttpWebRequest 消耗 Web 服务的性能而言,与添加服务引用方法相比,速度会更快,因为在后一种情况下,该过程涉及运行时和运行时的对象构造。此外,它还带来许多其他开销(例如 Web 服务的其他 Webmethods 部分),您可能不会在特定上下文中使用它。
更新:
看来我的猜测是错误的。
在此 https://web.archive.org/web/20210619192654/https://www.4guysfromrolla.com/articles/022410-1.aspx 有一些关于 SOAPcall 与服务引用的统计数据方法。测试表明,服务引用方法要快得多。
I think in terms of performance consuming webservice through HttpWebRequest would be faster when compared with Add service reference approach as in the later case the process involves object construction at runtime & further it carries lot of other overheads (for instance other webmethods part of web service) which you may not be using it in that particular context.
UPDATE:
Looks like my guess was wrong.
In this https://web.archive.org/web/20210619192654/https://www.4guysfromrolla.com/articles/022410-1.aspx there are some statss on SOAPcall vs service reference approach. The test revealed that service reference approach is much faster.
添加引用只能通过 Web 服务 URL(.net amsx 服务、wcf 服务和其他基于 SOAP 的服务,如 java、php 或 ruby 等)来完成。添加引用会生成存根,其中包含调用 Web 服务所需的所有类。它包括作为参数传递或从 Web 方法返回的所有对象类型。
另一方面,HttpWebRequest 不仅可以用于调用 Web 服务,还可以用于调用简单的 aspx 页面、HTML 页面或任何基于 HTTP 或 HTTPS 的 url。就像在浏览器中点击 URL 一样。
Add reference can only be done with web service URLs (.net amsx services, wcf services, and other SOAP based services like in java,php or ruby etc). Adding a reference generates stub which contains all the classes necessary for calling web services. It includes all object types that are passed as parameters or returned from web methods.
On the other hand HttpWebRequest can be used not only to call webservices but simple aspx pages, HTML pages or any HTTP or HTTPS based urls. Its just like hitting a URL in browser.