F
F
Franco Mannino2017-09-07 15:21:14
C++ / C#
Franco Mannino, 2017-09-07 15:21:14

There is no page, but the server returns "200", how to understand in this situation that the page is still missing?

For example, with this url: https://XN--80APMGLWL.XN--P1AI/in/Moscow/Mebelnaya_...
In fact, there are only 21 pages. The rest are displayed empty.
How in this situation to understand that the page still does not exist?

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();
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tom Nolane, 2017-09-07
@garinov

3a42bbeb01de4099b5dc0464d9b3331a.PNG
there is a page ... why is it not there?
if you specify
then there really is no page, and (for this site) statusCodethere will be a 302 (redirect to https://office.rf/404.htm )
1) change request.Method from HEAD to GET
2) full code:

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();

profit

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question