创建了简单的Restful服务用于登录验证。以下是我的接口和类定义。
界面 IDemo:
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?用户名=演示&密码=演示(带 http://)
当我尝试在 Fiddler2 中的 Post 方法下解析上述 URL 时,我收到错误请求HTTP400错误。
谁能帮助我我的代码中有什么问题。
谢谢和问候,
维杰
您的 URI 模板看起来像您正在发送 URL 中的参数。但是当你使用 POST 时,参数会在 http 正文中发送。
请注意,您不应在 url 中发送用户名和密码,因为它可能会被记录。
对于上述 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"