How to use get the response content html and save it to text file? [Answered]RSS

1 reply

Last post Jan 12, 2018 06:16 AM by Yuk Ding

  • How to use get the response content html and save it to text file?

    Jan 11, 2018 05:02 PM|suchanewbee|LINK

    Hi,

    I'm learning how to use asp to develop a web crawler. So I need to get the repsonse html and save it to text file. Then I can analyze the html based on the saved data. Does anyone have idea about how to achieve this? Thanks in advance.

  • Re: How to use get the response content html and save it to text file?

    Jan 12, 2018 06:16 AM|Yuk Ding|LINK

    Hi suchanewbee,

    This link provide a sample for the web crawler:

    https://www.example-code.com/asp/spider_simpleCrawler.asp

    In addition, I could provide the asp.net application sample:

     string targetpath = @"d:\webcrawler\test.txt";
                Console.WriteLine("Please enter the target URL:");
                string URI = Console.ReadLine();
                WebRequest myrequest = WebRequest.Create(URI);
                WebResponse myresponse=  myrequest.GetResponse();
                Stream datastream = myresponse.GetResponseStream();
                StreamReader reader = new StreamReader(datastream, Encoding.Default);
                if(!File.Exists(targetpath))
                {
                    File.Create(targetpath);
                };
    
                string htmlcontent = reader.ReadToEnd();
                File.WriteAllText(targetpath,htmlcontent);

    Best Regards,

    Yuk Ding

    Yuk Ding

    MSDN Community Support
    Please remember to "Mark as Answer" the responses that resolved your issue.