Search code examples
c#uwpwindows-10-mobile

Redirected WebRequest with cookie not working (Windows Build 15063)


I've created an UWP application that calls a Webservice which response with a redirect combined with cookie.

This worked with Windows 10, version 1803 (Build 17134)
When switching to Windows 10 Creators Update (Build 15063) so it will run on Windows 10 Mobile it stopped working (On PC and Mobile)

Using fiddler one can see that the cookie was not used when the request was redirected anymore.

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;
            }
        }
    }
}

Any idea how to get this working on Windows Phone?


Solution

  • I have found out that HttpClient has a constructor that takes a HttpMessageHandler. While HttpRequest did not (yet) had the option to disable automatic redirects, this can be set on the 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();
        // ...
     }