【问题标题】:Restful WCF service POST methods returns HTTP400 error in fiddler2Restful WCF 服务 POST 方法在 fiddler2 中返回 HTTP400 错误
【发布时间】:2012-06-16 10:34:21
【问题描述】:

为登录验证创建了简单的 Restful 服务。以下是我的接口和类定义。

界面演示:

public interface IDemo
{
    [OperationContract]
    [WebInvoke(RequestFormat = WebMessageFormat.Json,
               ResponseFormat = WebMessageFormat.Json,
               BodyStyle = WebMessageBodyStyle.Bare,
               UriTemplate = "/ValidateUser?Username={UserName}&Password={Password}",
               Method = "POST")]
    string  ValidateUser(string Username, string Password);
}

课堂演示:

public class Demo:IDemo
{
    public string ValidateUser(string Username, string Password)
    {
        Users objUser = new Users();
        objUser.UserID = Username;
        objUser.Password = Password;
        string Msg = LoginDataService.ValidateUser(Username, Password);
        return Msg;
    }
}

localhost:49922/Demo.svc/ValidateUser?Username=demo&Password=demo(带http:\)

当我尝试在 Fiddler2 的 Post 方法下解析上述 URL 时,出现 Bad Request HTTP400 错误。

谁能帮我看看我的代码出了什么问题。

感谢和问候,

维杰

【问题讨论】:

  • 我不知道您的代码可能还有什么其他问题,但是如果您要在查询字符串中指定所有输入参数,您应该将您的服务方法标记为 GET 而不是 POST .
  • 如何配置适用的端点以及客户端和服务是否相同?
  • 巴吉感谢您的评论。这是一个简单的演示服务,仅此而已..

标签: wcf post fiddler wcf-rest


【解决方案1】:

对于上述 REST 方法,来自 Fiddler 的 POST 需要如下所示:

POST http://localhost/Sample/Sample.svc/ValidateUser?Username=demo&Password=demo HTTP/1.1
User-Agent: Fiddler
Content-Type: application/json
Host: rajeshwin7
Content-Length: 0

这样做我会返回 200 OK HTTP 状态,如下所示:

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 44
Content-Type: application/json; charset=utf-8
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Thu, 14 Jun 2012 15:35:28 GMT

"Server returns username demo with password demo"

【讨论】:

  • 我已将无参数列表的方法更改为“/validate”,并且我已使用 testclient 应用程序对其进行了测试,它工作正常。但是当我用提琴手测试这个时,我无法得到 json 结果。谢谢你的评论...
  • 你能发布你的提琴手原始请求和响应
【解决方案2】:

您的 URI 模板看起来像是在发送 URL 中的参数。但是当您使用 POST 时,参数会在 http 正文中发送。

请注意,您不应在 url 中发送用户名和密码,因为它可以被记录。

【讨论】:

    猜你喜欢
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多