[英]restsharp - no cookie in response object
I have an issue with one of my requests to localhost server. 我对localhost服务器的一个请求有问题。
To authenticate, I need two cookies, one from sendReqForToken() method and one from sendLoginReq(string login, string pass). 要进行身份验证,我需要两个cookie,一个来自sendReqForToken()方法,另一个来自sendLoginReq(字符串登录,字符串传递)。
In response I get cookie from sendLoginReq, but not from sendReqForToken(). 作为响应,我从sendLoginReq获取cookie,但不从sendReqForToken()获取cookie。
I don't have idea why one request has a cookie second doesn't. 我不知道为什么一个请求有一个cookie第二个没有。
It is interesting that I get correct token(response content is correct) from sendReqForToken() method, but without any cookie in response header. 有趣的是,我从sendReqForToken()方法获得了正确的令牌(响应内容是正确的),但在响应头中没有任何cookie。
This is sendReqForToken() method body: 这是sendReqForToken()方法体:
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 always is equal to 0. response.cookies property always is equal to null. response.Cookies.Count总是等于0. response.cookies属性总是等于null。
This is sendLoginReq method body: 这是sendLoginReq方法体:
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
{
}
});
}
In second method I get correct cookie. 在第二种方法中,我得到正确的cookie。
Thanks a lot for any ideas. 非常感谢任何想法。
Thanks @KarthikNishanth. 谢谢@KarthikNishanth。 To make it clear:
说清楚:
client.CookieContainer = new CookieContainer ();
var cookie = client.CookieContainer.GetCookieHeader(new Uri("http://domain_or_subdomain.ext"));
var client
is a RestClient
var client
是一个RestClient
After the client.Execute(request);
client.Execute(request);
the GetCookieHeader()
will return the desired cookie GetCookieHeader()
将返回所需的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. 我遇到了同样的问题,你的服务器给你发送了一个带有HTTPonly = true参数的cookie,你应该将HTTOnly参数改为false然后你可以从令牌响应中获取cookie。
see this link answer to your question 看到这个链接回答你的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.