通过 https 调用 Web 服务
在 C# 中通过 https 调用 Web 服务需要什么? 我需要从网站获取证书吗?我如何使用它来调用网络服务?
What do I need to call a web service over https in C#?
Do I need to get the certificate form the site? How do I use this to call the web service?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过
https
调用 Web 服务与通过http
调用 Web 服务没有什么特别或不同的地方。您可以使用 svcutil.exe (或在 VS 中添加服务引用)或 wsdl.exe 并调用该方法。较低级别的类 HttpWebRequest 和 HttpWebResponse 最终将处理实际的调用和证书,但它应该对您的代码保持透明。当然,托管 Web 服务的服务器需要提供有效的证书。There's nothing special or different for calling a web service over
https
than overhttp
. You generate a client proxy from the WSDL using either svcutil.exe (or Add Service Reference in VS) or wsdl.exe and invoke the method. The lower level classes HttpWebRequest and HttpWebResponse will eventually take care of the actual call and certificates but it should be transparent for your code. Of course the server hosting the web service needs to provide a valid certificate.我认为您正在使用 Visual Studio 来创建项目,如果您使用的话,这很容易做到。我认为您拥有想要连接的 Web 服务的 URL,并且它以 HTTPS 开头。
在解决方案资源管理器的项目中(假设您使用 Visual Studio),您应该看到一个节点显示“引用”,另一个节点显示“Web 引用”。右键单击“Web Reference”,然后基本上按照向导进行操作。这非常简单。您可以指定自己的命名空间。我通常使用 SomethingAPI 格式。然后像您希望项目中的任何其他对象一样使用该 API。您将获得智能感知等一切。
I take that you are using Visual Studio to create your projects, if you are it is pretty easy to do. I take that you have the url for the web service that you would like to connect to and it starts with HTTPS.
In your project in the solution explorer (assuming you using Visual Studio), you should see a node saying "References" and another one saying "Web References". Right click on the "Web Reference" and then basically follow the wizard. It is pretty straight forward. You can spec your own Namespace. I usually use the format SomethingAPI. Then use the API as you would like any other object in your project. You will get the intellisense and all.
不过,某些证书可能会出现已知问题。请参阅http://support.microsoft.com/kb/823177/en-us
There might occur known problems with some certificates though. See http://support.microsoft.com/kb/823177/en-us
您是否拥有 Web 服务提供商提供的客户端证书?
如果是这样,根据您使用的 .NET 版本,有多种不同的方法可以实现此目的。您使用的是什么版本,生成客户端代理类的方式是否受到限制?
Do you have a client certificate that has been supplied by the provider of the web service?
If so, there are various different ways of doing this depending upon which version of .NET you are using. What version are you using, and are you limited in how you can generate your client proxy classes?