Exception on 302 redirect HTTPS to HTTP after POST in Windows Phone 8.1

0

I'm attempting to create a Windows Phone 8.1 app that logs in to a web site using a POST request (using Windows.Web.Http), then continues to another page. There's of course a lot more code in there but I don't think it's germane to this discussion.

HttpClient httpClient = new HttpClient();
...
HttpResponseMessage myHttpPostResponse = await httpClient.PostAsync(myUri,postContent);

The challenge that I have not been able to overcome is that an exception is thrown before a response is received. This is the message I get in the Immediate Window:

A first chance exception of type 'System.Exception' occurred in mscorlib.ni.dll
System.Exception: Exception from HRESULT: 0x80072F08
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at App1.MainPage.<getSJWasync>d__1.MoveNext()

My searches have led me to believe that this is caused by the web server doing a 302 redirect from the https login page to a non-secure http page...

Found on MSDN...

"ERROR_INTERNET_HTTPS_TO_HTTP_ON_REDIR 12040 (2F08) The application is moving from an SSL to an non-SSL connection because of a redirect."

Using the Chrome Developer Tools on my PC I can see that indeed there is a 302 redirect after the POST. I have attempted to manually handle the redirect by not allowing auto-redirects (in HttpBaseProtocolFilter) but that exception is still thrown. I have tried to handle the exception using try / catch but I don't know how to still get an HttpResponseMessage.

c#
windows-phone-8.1
http-redirect
hresult
asked on Stack Overflow Jan 7, 2015 by Porcupine911 • edited Feb 7, 2016 by Porcupine911

1 Answer

1

Disabling auto-redirect in HttpBaseProtocolFilter should stop HttpClient from throwing exception.

Here is an example:

Uri uri = new Uri(
    "https://http2.cloudapp.net/?status=302&name=Location&value=http%3a%2f%2fkiewic.com");
HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();

// Only needed to be able to connect to test server.
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted);

// Turn off auto-redirections.
filter.AllowAutoRedirect = false;

HttpClient client = new HttpClient(filter);
HttpStringContent content = new HttpStringContent("blah");
HttpResponseMessage response = await client.PostAsync(uri, content);

// Redirect URL is here.
Debug.WriteLine(response.Headers["Location"]);
answered on Stack Overflow Jan 7, 2015 by kiewic • edited Jan 7, 2015 by kiewic

User contributions licensed under CC BY-SA 3.0