WebRequest 没有被重定向
WebRequest is not redirected
方法:
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 是 http://monosnap.com/page/faq , which must be redirected to the some language area, for example - http://monosnap.com/ru/page/faq
但它抛出 WebException,消息 "The remote name could not be resolved: 'page'"。
更新:我添加了用户代理
request.UserAgent = "Accept-Language: en-US,en;q=0.5";
但我仍然遇到同样的异常"The remote name could not be resolved: 'page'"
此外,重定向在我的浏览器中正常工作。所以,问题出在代码上。
对于您尝试访问的站点,添加 "Accept-Language" header 就足够了:
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();
}
}
}
示例输出:
OK 186
如果无法确定语言,则重定向器似乎没有默认设置。
有关设置 Accept-Language 的更多信息:Setting language preferences in a browser。
方法:
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 是 http://monosnap.com/page/faq , which must be redirected to the some language area, for example - http://monosnap.com/ru/page/faq
但它抛出 WebException,消息 "The remote name could not be resolved: 'page'"。
更新:我添加了用户代理
request.UserAgent = "Accept-Language: en-US,en;q=0.5";
但我仍然遇到同样的异常"The remote name could not be resolved: 'page'" 此外,重定向在我的浏览器中正常工作。所以,问题出在代码上。
对于您尝试访问的站点,添加 "Accept-Language" header 就足够了:
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();
}
}
}
示例输出:
OK 186
如果无法确定语言,则重定向器似乎没有默认设置。
有关设置 Accept-Language 的更多信息:Setting language preferences in a browser。