using System;
using System.Net;
namespace ResponseTest
{
class Program
{
static void Main(string[] args)
{
string url = "https://XN--80APMGLWL.XN--P1AI/в/Москва/Мебельная_фурнитура/стр_210";
int statusCode;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AllowAutoRedirect = false;
request.Method = WebRequestMethods.Http.Head;
request.Accept = @"*/*";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
statusCode = (int)response.StatusCode;
response.Close();
}
catch (WebException ex)
{
if (ex.Response == null)
throw;
statusCode = (int)((HttpWebResponse)ex.Response).StatusCode;
}
Console.WriteLine(url);
Console.WriteLine(statusCode);
Console.ReadKey();
}
}
}
string url = "https://XN--80APMGLWL.XN--P1AI/в/Москва/Мебельная_фурнитура/стр_11210";
statusCode
будет 302 (редирект на https://офирмах.рф/404.htm )string url = "https://XN--80APMGLWL.XN--P1AI/в/Москва/Мебельная_фурнитура/стр_11210";
int statusCode;
string html;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AllowAutoRedirect = false;
request.Method = WebRequestMethods.Http.Get;
request.Accept = @"*/*";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
statusCode = (int)response.StatusCode;
using (StreamReader stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
html = stream.ReadToEnd();
}
response.Close();
if(!html.Contains("card")) Console.WriteLine("страницы не существует");
else Console.WriteLine("страница существует!");
}
catch (WebException ex)
{
if (ex.Response == null)
throw;
statusCode = (int)((HttpWebResponse)ex.Response).StatusCode;
}
//Console.WriteLine(url);
//Console.WriteLine(statusCode);
Console.ReadKey();