【问题标题】:Getting an http 400 error on calling a restful wcf service using http post使用 http post 调用 restful wcf 服务时出现 http 400 错误
【发布时间】:2011-04-08 02:43:09
【问题描述】:

我已经设置了一个 wcf 服务。不幸的是,当我通过提琴手、网络或其他任何地方调用该服务时,我得到一个 http 400 错误。我真的很困惑从哪里开始解决这个问题。任何建议表示赞赏。 WSDL 目前不是一个选项。 REST 是一项要求。我之前可以使用 GET 进行调用,但是,get 并不安全,因为我需要传递用户 ID 和密码才能进行验证,而且很容易查看。

wcf服务有如下接口:

    [WebInvoke(UriTemplate = "/Login", Method="POST", 
        ResponseFormat = WebMessageFormat.Xml, 
        BodyStyle = WebMessageBodyStyle.Wrapped)]
    [OperationContract]
    bool Login(string UserName, string PassWord, string AppKey);

该服务具有以下代码。是的,这段代码是不完整的,我知道 AppKey 真的没有做任何事情,它在这一点上作为一个占位符。

    public bool Login(string UserName, string PassWord, string AppKey)
    {
        bool bl = false;
        if (String.IsNullOrEmpty(AppKey))
        {
            throw (new ApplicationException(AppKeyNull));
        }
        if (Membership.ValidateUser(UserName, PassWord))
        {
            bl = true;
        }
        return (bl);
    }

我正在尝试通过提琴手调用该服务。我有以下网址:

http://127.0.0.1:82/WebServices/RemoteAPI.svc/Login

我有以下标题设置:

User-Agent: Fiddler
Host: 127.0.0.1:82
Content-Length: 46
Content-Type: application/x-www-form-urlencoded

我有以下身体设置:

UserName=xxxxxx&PassWord=yyyyyy&AppKey=blah

【问题讨论】:

    标签: wcf rest post


    【解决方案1】:

    试试这个:

    将您的方法签名和属性更改为此

    [WebInvoke(UriTemplate = "/Login", Method = "POST", ResponseFormat = WebMessageFormat.Xml, RequestFormat= WebMessageFormat.Xml)]
    [OperationContract]
    bool Login(LoginContract loginInfo);
    

    然后添加一个新的数据合约 LoginContract

    [DataContract]
    public class LoginContract
    {
        [DataMember]
        public string UserName { get; set; }
    
        [DataMember]
        public string PassWord { get; set; }
    
        [DataMember]
        public string AppKey { get; set; }
    
    }
    

    然后当你用 Fiddler 发帖时指定Content-Type: text/xml

    并将其用作您的请求正文

    <LoginContract xmlns="http://schemas.datacontract.org/2004/07/YourAppName" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <AppKey>blah</AppKey>
    <PassWord>testpwd</PassWord>
    <UserName>test</UserName></LoginContract>
    

    或者,如果您想使用 JSON 作为请求正文,请更改 RequestFormat = WebMessageFormat.JSON 并将其作为您的请求正文传递

    { "AppKey": "blah", "PassWord": "testpwd", "UserName": "test" }
    

    【讨论】:

      【解决方案2】:

      如果 Content-Length 稍有偏差,如果您出于某种原因自行计算,则可能会发生 400 错误。可能需要更多信息,例如 http 请求的全文。

      【讨论】:

      • 感谢您的建议。我手动计算了内容长度,也得出了 46。我让提琴手自己计算。不幸的是,这并没有解决问题。
      【解决方案3】:

      这是传递给 REST 服务的有效数据格式吗?

      我只能假设你正在使用这个: http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webinvokeattribute.aspx

      而且您没有指定支持 XML 和 JSON 的 RequestFormat。您的正文既不是 JSON 也不是 XML。

      【讨论】:

      • json 和 xml 是唯一支持的格式吗?我认为它们适用于复杂的对象。
      【解决方案4】:

      查看WCF REST Contrib 以获取相关帮助。或者,您可以在实现方法中接受 Stream 参数,并使用 HttpUtility.ParseQueryString 实用程序从 POST 正文中解析出查询字符串值。

      【讨论】:

        【解决方案5】:
        User-Agent: Fiddler
        Host: localhost:8889
        Content-Length: 113
        **Content-Type: text/xml**
        

        检查请求标头中是否缺少“Content-Type”?

        【讨论】:

          猜你喜欢
          • 2011-07-11
          • 2011-08-25
          • 1970-01-01
          • 1970-01-01
          • 2013-11-27
          • 1970-01-01
          • 1970-01-01
          • 2011-05-30
          • 2014-08-06
          相关资源
          最近更新 更多