提供的URI方案'http'无效; 预期'https'

Tar*_*ngh 26 c# wcf

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

"provided URI scheme'http' is invalid; expected 'https', Parameter name: Via"
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

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

<appSettings>
<add key="WEBSERVICE" value="URL"/>
Run Code Online (Sandbox Code Playgroud)

在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(); 
Run Code Online (Sandbox Code Playgroud)

当它试图调用TestMethod时,它给了我这个错误.

Jam*_*ley 56

您使用以下行将安全性设置为传输模式(HTTPS):

wb.Security.Mode = WebHttpSecurityMode.Transport;

baseAddressHTTP或HTTPS地址的值吗?

  • 那就是问题所在.您指定要安全传输,但HTTP不是安全传输. (5认同)
  • 此设置也可能出现在web.config中,如下所示:<security mode ="Transport"> ... </ security>在这种情况下,只需删除或评论整个安全部分,即可使用http而不是https. (4认同)