restsharp-响应对象中没有cookie [英] restsharp - no cookie in response object
问题描述
我对本地服务器的请求之一有问题。
I have an issue with one of my requests to localhost server.
要进行身份验证,我需要两个cookie,一个来自sendReqForToken()方法,一个来自sendLoginReq(字符串登录,字符串传递)。
To authenticate, I need two cookies, one from sendReqForToken() method and one from sendLoginReq(string login, string pass).
作为响应,我从sendLoginReq获取cookie,但没有从sendReqForToken()获取cookie。
In response I get cookie from sendLoginReq, but not from sendReqForToken().
我不知道为什么一个请求没有cookie的原因。
I don't have idea why one request has a cookie second doesn't.
有趣的是,我从sendReqForToken()方法获得了正确的令牌(响应内容正确),但是响应头中没有任何cookie。
It is interesting that I get correct token(response content is correct) from sendReqForToken() method, but without any cookie in response header.
这是sendReqForToken()方法主体:
This is sendReqForToken() method body:
public void sendReqForToken()
{
string adres = Globals.TOKEN_URL;
RestRequest request = new RestRequest(adres, Method.GET);
var client = new RestClient();
client.CookieContainer = new CookieContainer();
client.ExecuteAsync(request, (response) =>
{
if (response.StatusCode == HttpStatusCode.OK)
{
var tokenValue = JsonConvert.DeserializeObject<Token.RootObject>(response.Content);
DataManager.Instance.authToken = tokenValue.authenticity_token;
if (response.Cookies.Count > 0)
{
var cookie = response.Cookies.FirstOrDefault();
DataManager.Instance.cookieJar.Add(new Uri(Globals.TOKEN_URL), new Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain));
}
}
else
{
}
});
}
response.Cookies.Count始终等于0。response.cookies属性始终等于null。
response.Cookies.Count always is equal to 0. response.cookies property always is equal to null.
这是sendLoginReq方法主体:
This is sendLoginReq method body:
public void sendLoginReq(string login, string pass)
{
login = "admin";
pass = "admin";
string adres = Globals.LOGIN_URL;
RestRequest request = new RestRequest(adres, Method.POST);
var client = new RestClient();
request.RequestFormat = DataFormat.Json;
try
{
request.AddBody(new
{
authenticity_token = DataManager.Instance.authToken,
commit = "Login",
utf8 = true,
user_session = new
{
email = login,
password = pass
}
});
}
catch
{
}
client.ExecuteAsync(request, (response) =>
{
if (response.StatusCode == HttpStatusCode.OK)
{
if (response.StatusCode == HttpStatusCode.OK)
{
var cookie = response.Cookies.FirstOrDefault();
DataManager.Instance.cookieJar.Add(new Uri(Globals.LOGIN_URL), new Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain));
}
}
else
{
}
});
}
第二种方法得到正确的cookie。
In second method I get correct cookie.
非常感谢任何想法。
推荐答案
我遇到了同样的问题,您的服务器向您发送了一个问题带有HTTPonly = true参数的cookie,您应该将HTTOnly参数更改为false,然后可以从令牌响应中获取cookie。
I had the same problem, your server sends you a cookie with HTTPonly=true parameter, you should change HTTOnly parameter to false and then you can grab the cookie from token response.
查看此链接回答您的问题
这篇关于restsharp-响应对象中没有cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!