18

I'm trying to parse a wikispaces page but I'm not able to get the actual page. I'm not sure if its a HttpClient bug or some missing configuration.

This is my code:

HttpClientHandler handler = new HttpClientHandler();
handler.AllowAutoRedirect = true;
_httpClient = new HttpClient(handler);

HttpResponseMessage response = await _httpClient
    .GetAsync("http://medivia.wikispaces.com/Monsters");

When i run that code I get the StatusCode 302 and get sent to https://session.wikispaces.com/1/auth/auth?authToken=token. I expect the HttpClient to follow a 302 because I have AllowAutoRedirect = true.

This is the first time I've encountered this problem. It works fine with Postman and RestClient which is part of RestSharp.

Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147
Diego
  • 183
  • 1
  • 2
  • 12
  • Why not use `WebClient`? – Lei Yang Feb 23 '17 at 01:03
  • 1
    I am not sure if I understood it but if you don't want to follow the redirect you could simply set ```handler.AllowAutoRedirect = false;``` – giubueno Feb 23 '17 at 01:03
  • 2
    @LeiYang Because `WebClient` is old, horrible and needs to die a nasty, painful death! – DavidG Feb 23 '17 at 01:07
  • Why is it old? you mean .net framework version? – Lei Yang Feb 23 '17 at 01:07
  • @LeiYang http://stackoverflow.com/questions/20530152/need-help-deciding-between-httpclient-and-webclient – DavidG Feb 23 '17 at 01:08
  • im using HttpClient becouse its the standard one for net core... never had any problems before... @giubueno i need to redirect, the problem is that HttpClient might not redirect enough ive even set the MaxRedirect to a absurd number and still didnt fix it. – Diego Feb 23 '17 at 01:31
  • Hi @Diego, welcome to StackOverflow! I took the liberty of editing your post to make it easier to understand. Feel free to undo the edits if you feel I have changed the meaning of your question. – Nate Barbettini Feb 23 '17 at 01:42

2 Answers2

20

The reason the HttpClient isn't redirecting properly is because the site is redirecting you to HTTPS and then back to HTTP. A quick fix is to GET https://medivia.wikispaces.com/Monsters instead, which is a better idea anyhow:

HttpResponseMessage response = await _httpClient.GetAsync("https://medivia.wikispaces.com/Monsters");
// Works fine! :)

I was curious why it didn't work the first way, so I dug a little deeper. If you watch the network trace in a browser or a network client, this is what happens:

GET http://medivia.wikispaces.com/Monsters
302 https://session.wikispaces.com/1/auth/auth?authToken=token
302 http://medivia.wikispaces.com/Monsters?redirectToken=token

That 302 from an encrypted (HTTPS) connection to an unencrypted one is causing the HttpClientHandler to stop automatically following. From what I can tell, this is a security quirk of the Windows implementation of HttpClientHandler, because the Unix one didn't seem to care about the HTTPS->HTTP redirect in my informal testing.

Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147
-1

I have tried a lot of options to resolve WebClient error 302. Any example works fine, but this example works perfect. Let me share my example for the comunity.

HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync("www.google.com");

// Works fine
var resultBytes = response.Content.ReadAsByteArrayAsync().Result;

var writer = new BinaryWriter(new FileStream(strFilePath, 
FileMode.CreateNew, FileAccess.Write));
writer.Write(resultBytes);
writer.Close();
Neuron
  • 5,141
  • 5
  • 38
  • 59
  • While your answer may be a solution for "issues with 302 error on `WebClient`" the question of this thread is already using `HttpClient`. – Martin Schneider Aug 30 '21 at 14:57