带 cookie 的重定向 WebRequest 不起作用(Windows 内部版本 15063)

Redirected WebRequest with cookie not working (Windows Build 15063)

我创建了一个 UWP 应用程序,该应用程序调用 Web 服务,该 Web 服务通过重定向和 cookie 进行响应。

这适用于 Windows 10,版本 1803(内部版本 17134)
当切换到 Windows 10 Creators Update(内部版本 15063)时,它将 运行 在 Windows 10 Mobile 上停止工作(在 PC 和移动设备上)

使用 fiddler 可以看到,当请求被重定向时,cookie 不再被使用。

public static async System.Threading.Tasks.Task<double> GetCreditAsync(string number, string pun, System.Threading.CancellationToken cancel = default(System.Threading.CancellationToken))
{
    var cookieContainer = new CookieContainer();

    var request = System.Net.WebRequest.Create("http://test.test") as HttpWebRequest;
    using (cancel.Register(() => request.Abort(), useSynchronizationContext: false))
    {
        request.Method = "POST";
        request.CookieContainer = cookieContainer;

        request.ContentType = "multipart/form-data; boundary=---------------------------7e23ca1f27119e";
        var data = "-----------------------------7e23ca1f27119e"
        + "\n" + "Content-Disposition: form-data; name=\"data1\""
        + "\n" + ""
        + "\n" + number
        + "\n" + "-----------------------------7e23ca1f27119e"
        + "\n" + "Content-Disposition: form-data; name=\"data2\""
        + "\n" + ""
        + "\n" + pun
        + "\n" + "-----------------------------7e23ca1f27119e--"
        + "\n" + "";
        var buffer = System.Text.Encoding.UTF8.GetBytes(data);
        using (var requeststream = await request.GetRequestStreamAsync())
            requeststream.Write(buffer, 0, buffer.Length);

        using (var response = (await request.GetResponseAsync()) as HttpWebResponse)
        {
            using (var responseStream = response.GetResponseStream())
            using (var stream = new StreamReader(responseStream))
            {
                var text = await stream.ReadToEndAsync();
                value = GetValue(text);
                return value;
            }
        }
    }
}

知道如何在 Windows Phone 上运行吗?

这似乎是新 UWP 版本中的一个已修复问题,请参阅有关此问题的详细信息hereFix HttpClient redirection logic on UAP.

要使Cookie在Windowsphone或以前的版本中正常工作,您可以尝试使用HttpCookieManager class and HttpCookie Class in Windows.Web.Http命名空间。

//To get/set cookies
using (var protocolFilter = new HttpBaseProtocolFilter())
{
    //get CookieManager instance
    var cookieManager = protocolFilter.CookieManager;
    //get cookies
    var cookies = cookieManager.GetCookies(uri);
    foreach (var cookie in cookies)
    {
        Debug.Write(cookie.Value);
    }
    //you can also SetCookie
    //cookieManager.SetCookie(MyCookie);
}

您可以使用 ING CookieUsageBehavior 属性.

定义 cookie 使用行为
var protocolFilter = new HttpBaseProtocolFilter()
//Do not handle cookies automatically. you can set it as your requirements.
protocolFilter.CookieUsageBehavior = HttpCookieUsageBehavior.NoCookies;
// Create a HttpClient
var httpClient = new HttpClient(protocolFilter);

我发现 HttpClient 有一个采用 HttpMessageHandler 的构造函数。虽然 HttpRequest (还) 没有禁用自动重定向的选项,但可以在 HttpClientHandler.

上设置
var cookieContainer = new CookieContainer();
using (var client = new HttpClient(new HttpClientHandler()
{
    CookieContainer = cookieContainer,
    UseCookies = true,
    AllowAutoRedirect = false
}))
{
    var content = new MultipartFormDataContent
        {
            { new StringContent(number), "data1" },
            { new StringContent(pun), "data2" },
        };

    var result = await client.PostAsync(RequestUriString, content, cancel);
    result = await client.GetAsync(RequestUriString, cancel);


    var text = await result.Content.ReadAsStringAsync();
    // ...
 }