使用 WCF 通过 Http 和 Https 调用 Web 服务

发布于 2024-07-23 08:28:49 字数 233 浏览 9 评论 0原文

在我们的项目中,我们有一个通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

狂之美人 2024-07-30 08:28:49

我在 MSDN 中找到了答案。

就我而言,我使用的是自定义绑定:

<customBinding>
    <binding name="jsonpBinding">
        <jsonpMessageEncoding/>
        <httpTransport manualAddressing="true"/>
    </binding>
</customBinding>

在服务中引用了该绑定,

<services>
    <service name="{YourInfoHere}">
        <endpoint address="" binding="customBinding" bindingConfiguration="jsonpBinding" behaviorConfiguration="{YourInfoHere}" contract="{YourInfoHere}"/>
    </service>
</services>

添加使用 httpsTransport 的第二个绑定,然后使用该绑定的第二个服务就可以了。 最终输出:

    <services>
        <service name="{YourInfoHere}">
            <endpoint address="" binding="customBinding" bindingConfiguration="jsonpBinding" behaviorConfiguration="{YourInfoHere}" contract="{YourInfoHere}"/>
            <endpoint address="" binding="customBinding" bindingConfiguration="jsonpBindingHttps" behaviorConfiguration="{YourInfoHere}" contract="{YourInfoHere}"/>
        </service>
    </services>
    <bindings>
        <customBinding>
            <binding name="jsonpBinding">
                <jsonpMessageEncoding/>
                <httpTransport manualAddressing="true"/>
            </binding>
            <binding name="jsonpBindingHttps">
                <jsonpMessageEncoding/>
                <httpsTransport manualAddressing="true" />
            </binding>
        </customBinding>
    </bindings>

可能不理想,但它有效。 这是我为使 SSL 发挥作用而所做的唯一更改。 因为这一切都在绑定中 运输,代码保持不变。

相关 MSDN 链接:

  1. 自定义绑定:http://msdn.microsoft.com/en- us/library/ms731377.aspx
  2. HttpTransport: http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.httptransportbindingelement.aspx
  3. HttpsTransport:http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.httpstransportbindingelement.aspx

I found an answer digging around MSDN.

In my case, I was using a custom binding:

<customBinding>
    <binding name="jsonpBinding">
        <jsonpMessageEncoding/>
        <httpTransport manualAddressing="true"/>
    </binding>
</customBinding>

That was referenced in the service

<services>
    <service name="{YourInfoHere}">
        <endpoint address="" binding="customBinding" bindingConfiguration="jsonpBinding" behaviorConfiguration="{YourInfoHere}" contract="{YourInfoHere}"/>
    </service>
</services>

Adding a second binding that used httpsTransport and then a second service that used that binding did the trick. Final output:

    <services>
        <service name="{YourInfoHere}">
            <endpoint address="" binding="customBinding" bindingConfiguration="jsonpBinding" behaviorConfiguration="{YourInfoHere}" contract="{YourInfoHere}"/>
            <endpoint address="" binding="customBinding" bindingConfiguration="jsonpBindingHttps" behaviorConfiguration="{YourInfoHere}" contract="{YourInfoHere}"/>
        </service>
    </services>
    <bindings>
        <customBinding>
            <binding name="jsonpBinding">
                <jsonpMessageEncoding/>
                <httpTransport manualAddressing="true"/>
            </binding>
            <binding name="jsonpBindingHttps">
                <jsonpMessageEncoding/>
                <httpsTransport manualAddressing="true" />
            </binding>
        </customBinding>
    </bindings>

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:

  1. Custom Binding: http://msdn.microsoft.com/en-us/library/ms731377.aspx
  2. HttpTransport: http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.httptransportbindingelement.aspx
  3. HttpsTransport: http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.httpstransportbindingelement.aspx
泡沫很甜 2024-07-30 08:28:49

我假设您正在使用 basichttpbinding。 然后你需要做两件事:

  • 将地址更改为https:)
  • 将安全模式设置为transport

I am assuming that you are using basichttpbinding. Then you need to do two things:

  • change the address to https :)
  • set the security mode to transport
浮萍、无处依 2024-07-30 08:28:49

据我所知,您正在使用 WCF 来构建通过 HTTPS 连接到远程 Web 服务的客户端。

为此,只需修改 WCF 支持的应用程序的客户端配置文件,替换 http://server.addresshttps://server.address,位于 configuration/system.serviceModel/client/endpoint/@address 中。 像这样:(

<configuration>
  <system.serviceModel>
    ...
    <client>
        <!-- change only the address string -->
        <endpoint address="https://server.name/Whatever"
            everything.else.stays.the.same />

    </client>
  </system.serviceModel>
</configuration>

配置文件的路径根据常规 .NET 规则而变化:无论是 ASPNET 应用程序还是服务等)

或者您可以在代码中显式设置地址:

    // instantiate new proxy to web service
    var svc= new ServiceClient();
    var e = svc.Endpoint;
    e.Address = new System.ServiceModel.EndpointAddress("https://server.address/JavaServiceUri");

我强烈建议您地址是可配置的,而不是对其进行硬编码。 这并不意味着它必须存储在 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:

<configuration>
  <system.serviceModel>
    ...
    <client>
        <!-- change only the address string -->
        <endpoint address="https://server.name/Whatever"
            everything.else.stays.the.same />

    </client>
  </system.serviceModel>
</configuration>

(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:

    // instantiate new proxy to web service
    var svc= new ServiceClient();
    var e = svc.Endpoint;
    e.Address = new System.ServiceModel.EndpointAddress("https://server.address/JavaServiceUri");

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.

寻找我们的幸福 2024-07-30 08:28:49

请参阅配置 HTTP 和 HTTPS

使用 Windows Communication Foundation
(WCF) over HTTP 要么需要
使用主机,例如互联网
信息服务 (IIS),或手册
HTTP 设置的配置
通过 HTTP 服务器 API。 这
文档手动描述
使用 HTTP 时配置 WCF
HTTPS。

另请参阅 需要的 WCF 绑定对于 HTTPS

我刚刚写完第一篇
生产 WCF 应用程序,其中
在我部署它之前工作得很好
到我们的生产环境。 所有的
突然之间,所有 WCF 调用都不再
工作,我会得到一个 JavaScript
“未定义 TestService”错误。
当我查看 JS 服务内部时
参考(在调试模式下),我得到了
以下错误:

找不到与端点的方案 http 匹配的基地址
与绑定 WebHttpBinding。
注册基地址方案是
[https]

显然我的 WCF 服务
将自己注册为 HTTPS(因为它
通过 SSL),但我的绑定只是
配置为 HTTP。 解决办法是
在您的内部定义自定义绑定
Web.Config 文件并设置安全性
模式为“运输”。 那么你只需
需要使用绑定配置
端点内的属性
定义指向您的自定义
捆绑。 整个HTTPS启用
system.serviceModel 部分如下:

Please see Configuring HTTP and HTTPS:

Using Windows Communication Foundation
(WCF) over HTTP either requires the
use of a host, such as Internet
Information Services (IIS), or manual
configuration of the HTTP settings
through the HTTP Server API. This
document describes manually
configuring WCF when using HTTP and
HTTPS.

And also see WCF Bindings Needed For HTTPS:

I just finished writing my first
production WCF application, which
worked very well until I deployed it
to our production environment. All of
a sudden none of the WCF calls would
work, and I would get a JavaScript
"TestService is not defined" error.
When I look inside the JS service
reference (in debug mode), I got the
following error:

Could not find a base address that matches scheme http for the endpoint
with binding WebHttpBinding.
Registered base address schemes are
[https]

So apparently my WCF service
registered itself as HTTPS (since it
is over SSL), but my binding was only
configured for HTTP. The solution is
to define a custom binding inside your
Web.Config file and set the security
mode to "Transport". Then you just
need to use the bindingConfiguration
property inside your endpoint
definition to point to your custom
binding. The entire HTTPS-enabled
system.serviceModel section is below:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文