-1

I am trying html-scraping for fun and my question is. This Response.write() method works from inside the try catch block on IDE. But it doesnt return the Response.write() method on the server.

try
          {
            if (x == 1)
            {
                Url = "https://www.**********.com/" + arama;
            }
            else
            {
                Url = "https://www.**********.com/" + arama + "?pagingOffset=" + y;
            }

            doc = web.Load(Url);
            liste = doc.DocumentNode.SelectNodes("//*[@id=\"searchResultsTable\"]/tbody/tr[" + i + "]/td[1]/a")[0].Attributes["href"].Value;
            Url = "https://www.**********.com" + liste;
            doc = web.Load(Url);
            liste = doc.DocumentNode.SelectNodes("//*[@id=\"classifiedDetail\"]/div[1]/div[2]/div[3]/div[1]/div/div[1]/h5")[0].InnerText;
            liste2 = doc.DocumentNode.SelectNodes("//*[@id=\"phoneInfoPart\"]/li/span[1]")[0].InnerText;
            count++;
            liste = liste.ToUpper();
            Response.Write(count + " - " + liste + " Tel: " + liste2 + "</br>");
        }
        catch { }

in short this web app using HtmlAgilityPack to find html values by their xpath and I use Response.write() to return the inside value while innertext() converts it to string for me.

  • Start by avoiding empty catch blocks that silently swallow any exception. I strongly suspect that an exception is being thrown, but you can't tell what because your code effectively says "I don't care if this didn't work. No-one needs to know what went wrong." – Jon Skeet Oct 22 '17 at 09:08
  • it says = "Object reference not set to an instance of an object." – Alperen Kaptan Oct 22 '17 at 14:26
  • Right, so you're getting a NullReferenceException. You should look at the stack trace to find out why, and read https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Jon Skeet Oct 22 '17 at 14:33
  • how do i read stack trace while the website on anotherhosting. because when i work with ide i dont get this error. – Alperen Kaptan Oct 22 '17 at 14:38
  • Well you've found out that the *message* is "Object reference not set to an instance of an object" - so however you got *that* information, just get it to show you the whole exception instead of just the message. – Jon Skeet Oct 22 '17 at 14:39
  • does this message related to the matter with any way? = "An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. " – Alperen Kaptan Oct 22 '17 at 14:50
  • Well yes - it's saying that an exception is being thrown, so the request isn't completing properly... – Jon Skeet Oct 22 '17 at 14:51
  • yeah i narrowed it down a little bit and found the problem. I have no idea how to solve this one yet. soo there is this Line 51: doc = web.Load(html); it suppose to load the html url to doc which is HtmlDocument type. My guess html string did not proparly rendered as suppose to be? i have the url like this... var html = @"https://www.MyName.com/" + arama; – Alperen Kaptan Oct 22 '17 at 15:10
  • Please don't start asking extra questions in comments. If that line is throwing the exception directly, then `web` is probably null - and we have no idea where that comes from. It sounds like you have more diagnostics work to do. – Jon Skeet Oct 22 '17 at 15:25

1 Answers1

1

Perhaps you get an exception in your web.Load call and it hits your empty catch? Try putting a Response.Write into your catch that just writes out "BOOM". Or better yet, write out the exception.

Todd
  • 44
  • 3
  • turns out to be the try catch works perfectly just doesnt return the response.write method. i need to print those values but how? – Alperen Kaptan Oct 22 '17 at 09:00