【问题标题】:RESTful GET on HTTPS returning bad request 400 errorHTTPS 上的 RESTful GET 返回错误请求 400 错误
【发布时间】:2013-11-13 00:49:47
【问题描述】:

我最近需要更改我的 REST WCF 服务以使用 SSL。 它托管在 IIS 6 中,在 SSL 要求之前运行良好。

我无法弄清楚为什么会收到 400 bad request 错误。 我有诊断日志,它说

<Message>The body of the message cannot be read because it is empty.</Message>

web.config 看起来像:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
        <webServices>
            <protocols>
            <add name="HttpGet"/>
        </protocols>
        </webServices>
        <identity impersonate="true"/>
        <customErrors mode="Off"/>
    </system.web>

    <startup>
        <supportedRuntime version="v4.0.30319"/>
    </startup>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IMessageService">
                    <security mode="Transport">
                        <transport clientCredentialType="None"/>
                    </security>
                </binding>
             </wsHttpBinding>
             <webHttpBinding>
                 <binding name ="webBinding">
                 </binding>
             </webHttpBinding>
             <basicHttpBinding>
                 <binding name="httpBinding">
                     <security mode="Transport">
                         <transport clientCredentialType = "None"/>
                     </security>
                 </binding>
             </basicHttpBinding>
        </bindings>
        <services>
            <service behaviorConfiguration="metadataBehavior"
                     name="XXXDataService.XXXDataService">
                <endpoint address="" 
                          binding="wsHttpBinding"
                          bindingNamespace="http://blah.blah.blah.com/XXXDataService/"
                          bindingConfiguration="WSHttpBinding_IMessageService"
                          contract="ZZZDataService.IZZZDataServices">
                </endpoint>
                <endpoint address="mex" 
                          binding="mexHttpsBinding" contract="IMetadataExchange" />
            </service>
        </services>
        <standardEndpoints>
            <webHttpEndpoint>
                <standardEndpoint name="" 
                                  helpEnabled="true" 
                                  automaticFormatSelectionEnabled="true" />
            </webHttpEndpoint>
        </standardEndpoints>
        <behaviors>
            <serviceBehaviors>
                <behavior name="metadataBehavior">
                    <serviceMetadata httpsGetEnabled="true" httpGetEnabled="false"/>
                    <serviceDebug includeExceptionDetailInFaults="True" 
                                  httpHelpPageEnabled="True"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
    </system.serviceModel>
</configuration>

我只删除了诊断部分,需要通过替换为 XXX 和 ZZZ 来保护名称。

任何帮助或建议将不胜感激。

服务实现:

[ServiceBehavior(Namespace="blah.blah.blah.com", Name="XXXDataService")] 
public class YYYDataService : IYYYDataService 
{ 
    public string GetUser(string id) 
    { 
        string result = string.Empty; 
        using (YYYAdmintTree tree = new YYYAdmintTree()) // used for accessing DB 
        { 
            result = tree.GetUserById(id, 1); 
        } 
        return result; 
    }
} 

合同:

[ServiceContract(Namespace="https://blah.blah.blah.com", Name="XXXDataService")]
public interface IYYYDataService
{
    [OperationContract]
    [WebGet(UriTemplate="/GetUser/{id}", ResponseFormat=WebMessageFormat.Json)]
    string GetUserById(string id);
}

【问题讨论】:

  • 你在 IIS 中设置了绑定来接收 https 请求吗?
  • IIS:需要安全通道 (SSL) - 已选中。选择忽略客户端证书。当我浏览到服务 svc 文件时,它会返回通用页面和 wsdl 的链接。

标签: wcf rest https


【解决方案1】:

当您将错误的参数作为输入传递时,会出现错误 400。如果您再次传递 XML 或 JSON 作为输入检查。如果服务器无法找到它,它会给你错误 404。但 400 肯定表明提供的输入有问题。

【讨论】:

  • 请求是通过浏览器中的 URL 发出的,参数作为 URL 的一部分 - https:blah.blah.com/service/service.svc/GetUser/123
  • 它应该可以工作,只需向我提供方法详细信息和您传递的参数方式。
  • 这是服务的实现:[ServiceBehavior(Namespace="blah.blah.blah.com", Name="XXXDataService")] public class YYYDataService : IYYYDataService { public string GetUser(string id) { string result =字符串。空; using (YYYAdmintTree tree = new YYYAdmintTree()) // 用于访问 DB { result = tree.GetUserById(id, 1); } 返回结果; } }
  • public string GetUser(string id) { 更改为 public string GetUser(ParamParam("id") string id) { 并从客户端传递值,例如 localhost:8080/whateverthespecifiedpathis/1 其中 1 是您希望传递的 id。我希望你能正确指定路径
猜你喜欢
  • 1970-01-01
  • 2017-12-07
  • 2019-07-28
  • 1970-01-01
  • 2019-03-13
  • 2018-04-07
  • 2018-05-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多