我正在使用Flurl.Http与可公开访问的网站https://studia3.elka.pw.edu.pl合作。 _restClient
是从PerBaseUrlFlurlClientFactory
获得的,地址为https://studia3.elka.pw.edu.pl/pl/
如果有某些要求,它应该寄回我一个cookie以作进一步使用。我在响应中收到了cookie(它位于标题部分),但是该cookie不存在于我的客户的cookie字典中(我认为应该在收到响应后出现在其中)
我用于发出请求和读取响应的代码如下:
private const string StudiaIdCookieName = "STUDIA_SID";
var cook = new Cookie(name: "STUDIA_COOKIES", value: "YES&", path: "/", domain: ".studia3.elka.pw.edu.pl");
var unauthenticatedCookiesRequest = _restClient.Request().WithCookie(cook);
var unauthenticatedCookiesResponse = await unauthenticatedCookiesRequest.SendAsync(verb: HttpMethod.Get);
if (!_restClient.Cookies.ContainsKey(key: StudiaIdCookieName))
throw new Exception(message: "Failed to get unauthenticated cookie from Studia");
var authenticateCookieRequest = _restClient.Request().AppendPathSegment(segment: LdapPathSegment);
var authenticateCookieResponse = authenticateCookieRequest.PostUrlEncodedAsync(data:
new { studia_login = username, studia_passwd = password });
问题出在
if (!_restClient.Cookies.ContainsKey(key: StudiaIdCookieName))
throw new Exception(message: "Failed to get unauthenticated cookie from Studia");
_restClient.Cookies仅包含我手动设置的cookie,而不包含响应中的cookie(响应cookie称为STUDIA_SID,当我在调试器中浏览响应时,它位于标题部分下)。
我的问题是:Cookies属性应该反映发出请求的更改吗?如果没有,我应该手动检查标题并从中提取Cookie吗?