1

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.

Aminion
  • 465
  • 1
  • 5
  • 13
  • Tested it in browser and it response with `302 Found...Location: /en/page/faq`. Also tested `http://monosnap.com/ru/page/faq` and page exists. check that code know how to handle redirects – Nkosi Oct 09 '16 at 17:13
  • 1
    Does it work if you set a user-agent (perhaps with a language included) so that the redirector knows what language to redirect to? [How to set User Agent with System.Net.WebRequest in c#](http://stackoverflow.com/q/33659663/1115360). – Andrew Morton Oct 09 '16 at 17:19
  • 1
    @AndrewMorton is correct. My browser includes `Accept-Language: en-US,en;q=0.5` when making request. Tested again by resending request with `Accept-Language: ru-RU,ru;q=0.5` and response was `302 Found...Location: /ru/page/faq` – Nkosi Oct 09 '16 at 17:24
  • Why are you putting the `Accept-Language` header in the field for the `User-Agent` header? – Roger Lipscombe Oct 09 '16 at 17:49

1 Answers1

4

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.

Further information on setting Accept-Language: Setting language preferences in a browser.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84