Handle redirect manually I built a class which uses an HttpClient instance for downloading the content of a page. This is working pretty well but I'm not really satisfied about my implementation of redirect. 

In fact, I handle the redirect manually as you can see:

/// &lt;summary&gt;
/// Web request handler
/// &lt;/summary&gt;
public class NetworkHelper
{
    /// &lt;summary&gt;
    /// Store the url.
    /// &lt;/summary&gt;
    private static Uri _storedUrl = null;

    /// &lt;summary&gt;
    /// Store the html data.
    /// &lt;/summary&gt;
    private static string _storedData = "";

    public static string GetHtml(Uri url)
    {
        if (url == _storedUrl)
            return _storedData;
        else
            _storedUrl = url;

        HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url);

        try
        {
            HttpClientHandler handler = new HttpClientHandler();
            handler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

            HttpClient httpClient = new HttpClient(handler);

            HttpRequestMessage request = new HttpRequestMessage
            {
                RequestUri = url,
                Method = HttpMethod.Get
            };

            HttpResponseMessage response = httpClient.SendAsync(request).Result;

            int statusCode = (int)response.StatusCode;

            if (statusCode &gt;= 300 &amp;&amp; statusCode &lt;= 399)
            {
                Uri redirectUri = response.Headers.Location;

                if (!redirectUri.IsAbsoluteUri)
                {
                    redirectUri = new Uri(request.RequestUri.GetLeftPart(UriPartial.Authority) + redirectUri);
                }

                return GetHtml(redirectUri);
            }

            using (WebClient wClient = new WebClient())
            {
                wClient.Encoding = System.Text.Encoding.UTF8;
                _storedData = wClient.DownloadString(url);
            }

            return _storedData;
        }
        catch (WebException)
        {
            throw;
        }
    }
}


How can I improve my implementation for handling the redirect?
Your code looks solid, however here are a few minor points.

Catching/throwing exceptions

You should only catch exceptions when you are going to handle them. There's no point in catching an exception when you're only going to (re)throw it. However if you were to use logging and still 'bubble up' the exception, you could go with this:

try
{
    //Your webrequest/response code...
}
catch (Exception ex)
{
    //DO YOUR LOGGING HERE

    throw;
}


But as of now I would remove the try/catch from the method and handle exceptions outside of it:

try
{
    string theHtml = NetworkHelper.GetHtml("http://example.com");
}
catch (WebException ex)
{
   //Handle the exception
}




Stored url and data

As of now, your code doesn't allow the same url twice in a row; it'll just return the stored data. This raises 2 questions/issues with me:


What if I want to refresh the result?
I can bypass the check easily but in a hacky way:


Pass url1
Pass url2 to change the _storedUrl variable
Call the method again with url1



I think better would be to change the signature of the method and add a boolean (optional) parameter to indicate if you want to refresh the result:

public static string GetHtml(Uri url, bool refresh = false)
{
    if (url == _storedUrl &amp;&amp; !refresh)
        return _storedData;
    else
        _storedUrl = url;

    //Rest of the logic...
 }


Now you will only return the _storedData if you have the same url and don't want to refresh.

Hope this helps!