Cookies Windows.Web.HttpClient:将不发送Cookie
我向主机的登录端点发送带有基本身份验证的HTTP Get请求:Cookies Windows.Web.HttpClient:将不发送Cookie,cookies,httpclient,Cookies,Httpclient,我向主机的登录端点发送带有基本身份验证的HTTP Get请求: request = new HttpRequestMessage(); // Configuration Item: Login URL Suffix request.RequestUri = new Uri(string.Format("https://{0}/{1}", Host, loginSuffix)); request.Met
request = new HttpRequestMessage();
// Configuration Item: Login URL Suffix
request.RequestUri = new Uri(string.Format("https://{0}/{1}", Host, loginSuffix));
request.Method = Windows.Web.Http.HttpMethod.Get;
var info = User + ":" + Password;
var token = Convert.ToBase64String(Encoding.UTF8.GetBytes(info));
request.Headers.Authorization = new HttpCredentialsHeaderValue("Basic", token);
_httpClient = CreateHttpClient(ref cookieManager);
response = await _httpClient.SendRequestAsync(request, HttpCompletionOption.ResponseContentRead);
response.EnsureSuccessStatusCode();
responseBodyAsText = await response.Content.ReadAsStringAsync();
HttpClient是使用筛选器创建的,以便稍后设置cookie:
private HttpClient CreateHttpClient(ref HttpCookieManager _cookieManager)
{
HttpBaseProtocolFilter _filter = new HttpBaseProtocolFilter();
HttpClient _httpClient = new Windows.Web.Http.HttpClient(_filter);
_cookieManager = _filter.CookieManager;
return _httpClient;
}
从响应中可以读取SET-COOKIE头
string[] Queries;
response.Headers.TryGetValue("Set-Cookie", out tmpString);
if (tmpString != null)
Queries = tmpString.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
我正在寻找一个具有定义名称(CookieKeyName)的Cookie,该名称将在下一个请求中设置
foreach (var query in Queries)
{
if (query.Contains(CookieKeyName))
{
staticCookieKey = query.Substring(0, query.IndexOf("="));
staticCookieValue = query.Substring(query.IndexOf("=") + 1);
}
}
我预计,HttpClient将在对该URL的响应中使用接收到的集合Cookie,作为每个后续请求中的Cookie自动使用
我正在准备下一个请求并自己设置Cookie:
request.RequestUri = new Uri(string.Format("https://{0}/qcbin", Host));
request.Method = Windows.Web.Http.HttpMethod.Get;
HttpCookie _cookie = new Windows.Web.Http.HttpCookie(staticCookieKey, Host, "/");
_cookie.Value = staticCookieValue;
bool replaced = cookieManager.SetCookie(_cookie);
以下请求的发送提供给Web异常401,因为服务器期望此URL与之前收到的响应Cookie中的URL相同
response = await _httpClient.SendRequestAsync(request, HttpCompletionOption.ResponseContentRead);
response.EnsureSuccessStatusCode();
responseBodyAsText = await response.Content.ReadAsStringAsync();
第二个请求不包含Cookie头。即使在CookieManager中设置Cookie,也没有在HttpClient的第一个响应中继续设置Cookie i
提示:Cookie值的长度约为6000个字符(来自IBM Data Power)
事先谢谢你的帮助