使用 WCF 通过 Http 和 Https 调用 Web 服务
在我们的项目中,我们有一个通过 http 和 https 运行的 java Web 服务。 我们希望在内部使用 http,在外部版本的 Web 应用程序中使用 https。
因此,我们在应用程序中创建了代理类,并在 web/app.config 中设置了 http 的绑定,一切正常。
我们需要对代码和配置进行哪些更改才能支持外部应用程序中相同服务的 https? 如果可以的话请提供代码片段来解释!
In our project we have a java web service that runs over http and https.
We want to use http internally and https for the external version of our web app.
So we've created the proxy class in our application and we have setup the binding for http in the web/app.config and all works fine.
What changes would we need to make to the code and the configuration to support https for the same service in our external application? If possible please supply code snippets to explain!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在 MSDN 中找到了答案。
就我而言,我使用的是自定义绑定:
在服务中引用了该绑定,
添加使用 httpsTransport 的第二个绑定,然后使用该绑定的第二个服务就可以了。 最终输出:
可能不理想,但它有效。 这是我为使 SSL 发挥作用而所做的唯一更改。 因为这一切都在绑定中 运输,代码保持不变。
相关 MSDN 链接:
I found an answer digging around MSDN.
In my case, I was using a custom binding:
That was referenced in the service
Adding a second binding that used httpsTransport and then a second service that used that binding did the trick. Final output:
May not be ideal, but it works. These were the only changes I made to make SSL work. Since it is all in the binding & transport, the code remains the same.
Relevant MSDN links:
我假设您正在使用 basichttpbinding。 然后你需要做两件事:
I am assuming that you are using basichttpbinding. Then you need to do two things:
据我所知,您正在使用 WCF 来构建通过 HTTPS 连接到远程 Web 服务的客户端。
为此,只需修改 WCF 支持的应用程序的客户端配置文件,替换 http://server.address 与 https://server.address,位于 configuration/system.serviceModel/client/endpoint/@address 中。 像这样:(
配置文件的路径根据常规 .NET 规则而变化:无论是 ASPNET 应用程序还是服务等)
或者您可以在代码中显式设置地址:
我强烈建议您地址是可配置的,而不是对其进行硬编码。 这并不意味着它必须存储在 app.config 中,但它应该是可以更改的。 代理也一样。
I understand that you are using WCF to build the client, that connects to a remote web service, over HTTPS.
To do this, just modify the client-side config file for the WCF-powered app, replacing http://server.address with https://server.address, in configuration/system.serviceModel/client/endpoint/@address . like so:
(The path to the config file varies according to the regular .NET rules: whether it is an ASPNET app, or a service, or etc.)
OR you can set the address explicitly in code:
I strongly advise you to make the address configurable, and not hard-code it. That does not mean it must be stored in app.config, but it should be changeable. The proxy, too.
请参阅配置 HTTP 和 HTTPS:
另请参阅 需要的 WCF 绑定对于 HTTPS:
Please see Configuring HTTP and HTTPS:
And also see WCF Bindings Needed For HTTPS: