Redirected WebRequest with cookie not working (Windows Build 15063)

80 Views Asked by At

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?

2

There are 2 best solutions below

0
lokimidgard On BEST ANSWER

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();
    // ...
 }
1
Breeze Liu - MSFT On

It seems a fixed issue in new UWP version, see the details about this issue here and Fix HttpClient redirection logic on UAP.

To make the Cookie work well in Windows phone or previous version, you can try to use the HttpCookieManager class and HttpCookie Class in Windows.Web.Http namespace.

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

You can define the cookie usage behavior usING CookieUsageBehavior property.

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