重定向WebRequest,cookie无法正常工作(Windows Build 15063)

时间:2018-06-18 17:28:23

标签: c# uwp windows-10-mobile

我创建了一个UWP应用程序,它通过重定向和cookie结合调用Web服务。

这适用于Windows 10,版本1803(Build 17134)
当切换到Windows 10 Creators Update(Build 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上运行吗?

2 个答案:

答案 0 :(得分:2)

我发现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();
    // ...
 }

答案 1 :(得分:1)

在新的UWP版本中似乎是一个固定的问题,请参阅有关此问题的详细信息hereFix HttpClient redirection logic on UAP

要使Cookie在Windows手机或以前版本中正常运行,您可以尝试在HttpCookieManager命名空间中使用HttpCookie类和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);
}

您可以使用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);