【问题标题】:provided URI scheme'http' is invalid; expected 'https'提供的 URI 方案'http' 无效;预期“https”
【发布时间】:2010-12-14 00:43:26
【问题描述】:

我有一个托管在 IIS 6.0 中的 RESTful Web 服务,我可以在浏览器中浏览该服务。当我尝试通过客户端控制台应用程序访问相同的服务时,它给了我以下错误:

"provided URI scheme'http' is invalid; expected 'https', Parameter name: Via"

我的 WebService web.config 有这样的设置:

<system.serviceModel>  
<services>  
  <service behaviorConfiguration="ServiceBehavior" name="TestAPI">
    <endpoint address="" behaviorConfiguration="RESTFriendly" binding="webHttpBinding" contract="ITestAPI" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>     
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="RESTFriendly">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>

我的客户端应用程序有 App.config,我从中获取地址:

<appSettings>
<add key="WEBSERVICE" value="URL"/>

在 Main 方法中:

WebChannelFactory<ITestAPI> cf = new WebChannelFactory<IAPI>(baseAddress);
            WebHttpBinding wb =cf.Endpoint.Binding as WebHttpBinding;
            wb.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
            wb.Security.Mode = WebHttpSecurityMode.Transport;
            cf.Credentials.UserName.UserName = "usermane";
            cf.Credentials.UserName.Password = "password";

            ITestAPI channel = cf.CreateChannel();
            string msg = channel.TestMethod(); 

当它尝试调用 TestMethod 时,它给了我这个错误。

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    您正在使用以下行将安全设置为传输模式,即 HTTPS:

    wb.Security.Mode = WebHttpSecurityMode.Transport;

    baseAddress 的值是 HTTP 还是 HTTPS 地址?

    【讨论】:

    • 这就是问题所在。您指定需要安全传输,但 HTTP 不是安全传输。
    • 这个设置也可能出现在 web.config 中,像这样的元素中: ... 在这种情况下,只需删除或注释整个安全部分,以便能够使用 http 而不是 https。
    • 我也面临同样的问题
    • 这很有帮助。我在我的代码中做了类似的事情,以允许程序根据我的 URL 设置值处理 HTTPS 和 HTTP。我有一个配置为处理 HTTPS 的生产服务器和一个用于处理 HTTP(网络内部)的测试服务器: if (Settings.Default.URL.Contains("https://")) { binding.Security.Mode = BasicHttpSecurityMode 。运输; } else { binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; }
    猜你喜欢
    • 1970-01-01
    • 2012-04-09
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 1970-01-01
    • 2015-11-19
    • 2012-02-19
    相关资源
    最近更新 更多