我在Windows Phone设备上使用RestSharp客户端时遇到问题。从头开始,我在线托管了ASP.NET Web Api服务。我有一个请求用户地址:POST:http://my-service-url.com/token我发送电子邮件和密码作为正文参数,我在响应中得到201状态代码和一个cookie。当我用小提琴手做的时候一切正常。我也有我的API的功能测试,它正在使用RestSharp,它也正常工作:
[Given(@"I fill email and password with correct data and I click log in button")]
public void GivenIFillEmailAndPasswordWithCorrectDataAndIClickLogInButton()
{
//Delete user if he exists
_testUserHelper.DeleteTestUser(TestEmail);
//Create new activated user
Assert.IsTrue(_testUserHelper.CreateTestUser(TestEmail, TestPassword));
//Prepare client
var client = new RestClient(CarRentalsConstants.HostAddress);
var restRequest = new RestRequest("api/token", Method.POST) { RequestFormat = DataFormat.Json };
//Add parameters to request
restRequest.AddBody(new { Email = TestEmail, Password = TestPassword });
//Perform request
_response = client.Execute(restRequest);
}
[When(@"the log in login process finishes")]
public void WhenTheLogInLoginProcessFinishes()
{
Assert.AreEqual(HttpStatusCode.Created, _response.StatusCode, _response.Content);
Assert.IsNotNull(_response.Cookies.SingleOrDefault(q => q.Name == ".ASPXAUTH"););
}
上面的一个正常工作,并且cookie在响应对象中。
现在,我尝试在Windows手机上执行的操作如下:
var restRequest = new RestRequest("token", Method.POST) {RequestFormat = DataFormat.Json};
restRequest.AddBody(new {Email = email, Password = password});
myWebClient.ExecuteAsync(restRequest, (restResponse, handle) =>
{
switch (restResponse.StatusCode)
{
case HttpStatusCode.Created:
{
var cookie = restResponse.Cookies.SingleOrDefault(q => q.Name == ".ASPXAUTH");
successLogicDelegate(cookie);
}
break;
case HttpStatusCode.BadRequest:
{
HandleBadRequest(restResponse.Content, failureLogicDelegate);
}
break;
default:
msgBox.Show(StringResources.ServerConnectionError);
failureLogicDelegate(null);
break;
}
});
在这种情况下,响应返回“Created”状态代码,但cookie随后设置为null。我不知道这里发生了什么,但我相当确定服务器正在发送这个cookie,那么它会丢失在哪里?
任何帮助都将非常感激。