当我使用 POST 时,ServiceStack Json 客户端没有反序列化我的结果时遇到问题。get 方法将响应反序列化为 UserCredentials 对象没有问题,但是当我使用 POST 方法时,它返回一个具有所有 null 属性的对象。如果我将传递给 Post 方法的类型更改为字典,我可以看到它得到了正确的结果,并且我可以看到来自 Fiddler 的其余调用在 http 级别成功。
public UserCredentials Login(string uname, string pwd)
{
var url = "/login";
//note that I have to send a dictionary because if I sent a Login Object with username and password it erronously sends {Login:} in the raw request body instead of {"Uname":uname, "Password":password}
var login = new Dictionary<string, string>() { { "Uname", uname }, { "Password", pwd } };
var result = client.Post<UserCredentials>(url, login);
return result;
}
这是响应(这是来自服务器的正确和预期的 http 响应)
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 14 Mar 2013 12:55:33 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Vary: Accept-Encoding
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Content-Length: 49
{"Uid":1,"Hash":"SomeHash"}
这是 UserCredentials 类
public class UserCredentials
{
public long Uid;
public string Hash;
}