Aminion Aminion - 1 month ago 19
C# Question

WebRequest is not redirected

Method:

public RequestTest Test(string url)
{
var test = new RequestTest() { Url = url };
var sw = new Stopwatch();
var request = WebRequest.CreateHttp(test.Url);
request.AllowAutoRedirect = true;
request.Method = "HEAD";
request.UserAgent = "Accept-Language: en-US,en;q=0.5";
try
{
sw.Start();
using (var response = (HttpWebResponse)request.GetResponse())
{
sw.Stop();
test.Time = (int)sw.ElapsedMilliseconds;
test.StatusCode = response.StatusCode;
}
return test;
}
catch (WebException ex)
{
test.StatusCode = ((HttpWebResponse)ex.Response).StatusCode;
return test;
}
}


URL is http://monosnap.com/page/faq , which must be redirected to the some language area, for example - http://monosnap.com/ru/page/faq

But it throws WebException, with Message "The remote name could not be resolved: 'page'".

UPD: I add useragent

request.UserAgent = "Accept-Language: en-US,en;q=0.5";


But I still get the same exception "The remote name could not be resolved: 'page'"
Also, redirect works properly from my browser. So, problem is in code.

Answer

For the site you are attempting to access, it is sufficient to add an "Accept-Language" header like this:

using System;
using System.Diagnostics;
using System.Net;

namespace ConsoleApplication1
{
    class Program
    {

        public class RequestTest
        {
            public string Url { get; set; }
            public int Time { get; set; }
            public HttpStatusCode StatusCode { get; set; }
        }

        public static RequestTest Test(string url)
        {
            var test = new RequestTest() { Url = url };
            var sw = new Stopwatch();
            var request = WebRequest.CreateHttp(test.Url);
            request.AllowAutoRedirect = true;
            request.Method = "HEAD";
            request.Headers.Add("Accept-Language: ru-RU, en; q = 0.5");
            try
            {
                sw.Start();
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    sw.Stop();
                    test.Time = (int)sw.ElapsedMilliseconds;
                    test.StatusCode = response.StatusCode;
                }
                return test;
            }
            catch (WebException ex)
            {
                test.StatusCode = ((HttpWebResponse)ex.Response).StatusCode;
                return test;
            }
        }

        static void Main(string[] args)
        {
            var x =  Test("http://monosnap.com/page/faq");
            Console.WriteLine(x.StatusCode + " " + x.Time.ToString());
            Console.ReadLine();

        }
    }
}

Sample output:

OK 186

It appears that the redirector does not have a default setting if it cannot determine the language.

Comments