Getting HTML code for an external file

Last post 10-26-2008 1:25 AM by rockinthesixstring. 7 replies.

Sort Posts:

  • Getting HTML code for an external file

    01-05-2008, 11:22 PM
    • Member
      1 point Member
    • atomicice
    • Member since 12-31-2007, 1:46 PM
    • Posts 3

    I need to obtain the HTML code for an external file(somewhere hosted on the web) (raw text would do) and then save it to a text file on my computer.

    Please let me know how to proceed.

     

     

  • Re: Getting HTML code for an external file

    01-06-2008, 12:21 AM
    • All-Star
      23,604 point All-Star
    • azamsharp
    • Member since 06-11-2003, 9:36 AM
    • Houston,Texas
    • Posts 4,518

    You will need the URL of the file you want to read. Then you can use the FileStream object to read the file.

      FileStream  fs = File.OpenRead(Server.MapPath("test.txt")); 
          StreamReader sr = new StreamReader(fs);

         while(sr.Peek() > -1)
           {

                 // You can do whatever you want here
                  Response.Write(sr.ReadLine());
          }

    Once, you got the string you can use the StreamWriter to write the file on your machine.

     

    HighOnCoding
    Get high on ASP.NET articles, videos, podcasts and more!
  • Re: Getting HTML code for an external file

    01-06-2008, 6:51 AM
    • Member
      1 point Member
    • atomicice
    • Member since 12-31-2007, 1:46 PM
    • Posts 3

    I already had what you suggest. The problem is that using this method, it finds the virtual path to the file. What I need to read an external file.. for example.. i need to read the contents of www.hello.com/world.html

    How do i specify the external path for the file?

  • Re: Getting HTML code for an external file

    01-06-2008, 9:25 AM
    Answer
    • All-Star
      20,998 point All-Star
    • Jeev
    • Member since 11-24-2005, 12:49 PM
    • Posts 3,163

    You can use a webrequest to make a request to the url and then save the response to a text file

    http://msdn2.microsoft.com/en-us/library/system.net.webrequest.aspx

    Jeev
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    If you get the answer to your question, please mark it as the answer.
  • Re: Getting HTML code for an external file

    01-08-2008, 1:27 AM
    Answer

    Hi

    Based on my understanding, you want to retrieve the HTML from external web page and save it to file or display on the page.

    There are the codes achrieving it, you can try it:

        private void saveURL(string url)
        {
            if (!string.IsNullOrEmpty(url))
            {
                string content = "";
    
                System.Net.WebRequest webRequest = WebRequest.Create(url);
                System.Net.WebResponse webResponse = webRequest.GetResponse();
                System.IO.StreamReader sr = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
                content = sr.ReadToEnd();
                //save to file
                StreamWriter sw = new StreamWriter(Server.MapPath("webcontent.txt"));
                sw.Write(content);
                sw.Flush();
                //display it in this page
                Response.Write(content);
            }
        }

    You can call the method like:  saveURL("http://forums.asp.net/t/1201527.aspx"); Please pay attention on the format of URL, "http://" is necessary. Without it, you will encounter the error "The format of the URI could not be determined. "

    You can get more information at: http://www.west-wind.com/presentations/dotnetWebRequest/dotnetWebRequest.htm

    Hope this can help.


     


    Vince Xu
    Microsoft Online Community Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Getting HTML code for an external file

    01-09-2008, 12:34 AM
    • Member
      1 point Member
    • atomicice
    • Member since 12-31-2007, 1:46 PM
    • Posts 3

    Thanks.. it worked

  • Re: Getting HTML code for an external file

    10-25-2008, 4:22 PM
    • Member
      4 point Member
    • syruf
    • Member since 10-25-2008, 8:21 PM
    • Posts 2

     thank you very much Vince, this helped me a lot too

  • Re: Getting HTML code for an external file

    10-26-2008, 1:25 AM

    just wanted to say, this is a huge help for me too... Thanks a bundle.Yes

    Chase

    Visual Studio 2008
    ASP.NET 3.5 (Visual Basic)
    SQL Server Express

    Check out my blog
Page 1 of 1 (8 items)