how to save web page in ASP.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • minhtran
    New Member
    • Feb 2008
    • 28

    how to save web page in ASP.NET

    Hi all
    Please, anyone know how to save a webpage as .txt in ASP.NET (C#, VB.NET code) then read .txt from source code. It would be a great appreciation from me for any help. Thank you very much
  • kunal pawar
    Contributor
    • Oct 2007
    • 297

    #2
    do u want to read web page of any site. It is possible through Net class in asp.net

    Comment

    • minhtran
      New Member
      • Feb 2008
      • 28

      #3
      Originally posted by kunal pawar
      do u want to read web page of any site. It is possible through Net class in asp.net
      Thank you for your reply (Kunal), please, if you have a sample code, would you please, post it so I can follow that sample code to do it. Again, a big thanks to you Kunal (Or any one have any Idea, I appreciate for your help)

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        Ok, if I understand what you are asking, you want to know if you can make a web request, copy the source to a text file, and then later read that text file and output the source. If this is what you want, check out this website. Use the code in the Main method, but skip the Console.WriteLi ne(). Add theses lines to the end:

        Code:
        FileStream fs = new FileStream(@"c:\google.txt",FileMode.OpenOrCreate,FileAccess.Write);
        StreamWriter sw = new StreamWriter(fs);
        sw.Write(sb.ToString());
        sw.Close();
        fs.Close();
        Replace "google.txt " with the path of your choice.

        Now, as to using it later through a web request, use this:
        Code:
        FileStream fs = new FileStream(@"c:\google.txt", FileMode.Open, FileAccess.Read);
        StreamReader sw = new StreamReader(fs);
        string buffer = sw.ReadToEnd();
        Response.Write(buffer);
        Response.End();
        Replacing "google.txt " again with the path to the text file, Remember to include:
        using System.IO;

        Comment

        • minhtran
          New Member
          • Feb 2008
          • 28

          #5
          Originally posted by insertAlias
          Ok, if I understand what you are asking, you want to know if you can make a web request, copy the source to a text file, and then later read that text file and output the source. If this is what you want, check out this website. Use the code in the Main method, but skip the Console.WriteLi ne(). Add theses lines to the end:

          Code:
          FileStream fs = new FileStream(@"c:\google.txt",FileMode.OpenOrCreate,FileAccess.Write);
          StreamWriter sw = new StreamWriter(fs);
          sw.Write(sb.ToString());
          sw.Close();
          fs.Close();
          Replace "google.txt " with the path of your choice.

          Now, as to using it later through a web request, use this:
          Code:
          FileStream fs = new FileStream(@"c:\google.txt", FileMode.Open, FileAccess.Read);
          StreamReader sw = new StreamReader(fs);
          string buffer = sw.ReadToEnd();
          Response.Write(buffer);
          Response.End();
          Replacing "google.txt " again with the path to the text file, Remember to include:
          using System.IO;

          Thank you very much InsertAlien
          but I have a error at:
          HttpWebResponse response = (HttpWebRespons e)
          request.GetResp onse();
          (Error: unable to connect remote server)
          that means I can not save that web page. All what you said is Exactly what I wish to do. A big appreciation to you (InsertAlien), However you know why I have the error that I mentioned above. (Or any one can share the knowledge, please)
          This project is to Validation (ABN number in Australia), but we don't have a database updated as Australia Government has, so we validation ABN via their web page ("http://www.abr.busines s.gov.au"). Thank you very much for your help and Ideas.

          Comment

          • kunal pawar
            Contributor
            • Oct 2007
            • 297

            #6
            [code=vbnet]
            <%@ Import Namespace="syst em" %>
            <%@ Import Namespace="syst em.io" %>
            <%@ Import Namespace="syst em.Net" %>




            Dim webObj As New WebClient()

            Dim str As String = ""
            Dim byt As Byte()
            Dim strSearch As String = ""
            Dim strOp As String = ""
            Dim uft8Obj As New UTF8Encoding


            Dim strUrl As String = "http://expasy.org/cgi-bin/pi_tool1?"
            byt = webObj.Download Data(strUrl)
            strSearch = uft8Obj.GetStri ng(byt)

            webObj.Dispose( )
            [/code]
            following code to write text file
            [code=vbnet]
            'Open a file for writing
            Dim FILENAME as String = Server.MapPath( "Output.txt ")

            'Get a StreamReader class that can be used to read the file
            Dim objStreamWriter as StreamWriter
            objStreamWriter = File.AppendText (FILENAME)

            'Append the the end of the string, "A user viewed this demo at: "
            'followed by the current date and time
            objStreamWriter .WriteLine("A user viewed this demo at: " & DateTime.Now.To String())

            'Close the stream
            objStreamWriter .Close()[/code]
            Last edited by Frinavale; Apr 16 '08, 01:30 PM. Reason: added [code] tags

            Comment

            • Curtis Rutland
              Recognized Expert Specialist
              • Apr 2008
              • 3264

              #7
              Originally posted by minhtran
              HttpWebResponse response = (HttpWebRespons e)
              request.GetResp onse();
              (Error: unable to connect remote server)
              That sounds like a connectivity problem. Did you type the URL into your code correctly? Does your server have permission to access the internet, and is your firewall blocking access to the site you want to go to?

              Does this work if you load it up in Visual Studio and run it in debug mode?

              Comment

              • minhtran
                New Member
                • Feb 2008
                • 28

                #8
                Originally posted by kunal pawar
                <%@ Import Namespace="syst em" %>
                <%@ Import Namespace="syst em.io" %>
                <%@ Import Namespace="syst em.Net" %>




                Dim webObj As New WebClient()

                Dim str As String = ""
                Dim byt As Byte()
                Dim strSearch As String = ""
                Dim strOp As String = ""
                Dim uft8Obj As New UTF8Encoding


                Dim strUrl As String = "http://expasy.org/cgi-bin/pi_tool1?"
                byt = webObj.Download Data(strUrl)
                strSearch = uft8Obj.GetStri ng(byt)

                webObj.Dispose( )

                following code to write text file

                'Open a file for writing
                Dim FILENAME as String = Server.MapPath( "Output.txt ")

                'Get a StreamReader class that can be used to read the file
                Dim objStreamWriter as StreamWriter
                objStreamWriter = File.AppendText (FILENAME)

                'Append the the end of the string, "A user viewed this demo at: "
                'followed by the current date and time
                objStreamWriter .WriteLine("A user viewed this demo at: " & DateTime.Now.To String())

                'Close the stream
                objStreamWriter .Close()


                Thank you very much Kanal
                I just back to work after weekend. Today is Mon 14/ Apr/ 2008 in Sydney_Australi a)
                This is a big help from you as I still have a problem as "Unable to connect the remote server" . I will figure it out as "Not Blocking by Firewall, or Permission..." Big Thanks to you Kanal.

                Comment

                • minhtran
                  New Member
                  • Feb 2008
                  • 28

                  #9
                  Originally posted by insertAlias
                  That sounds like a connectivity problem. Did you type the URL into your code correctly? Does your server have permission to access the internet, and is your firewall blocking access to the site you want to go to?

                  Does this work if you load it up in Visual Studio and run it in debug mode?
                  Thank you InsertAliens
                  I just back to work after weekend. Today is Mon 14/ Apr/ 2008 in Sydney_Australi a)
                  YOU ARE RIGHT. Our server asks username and password b4 to reach any webpage as external communication. So I will solve this problem as to get permission for my coding directly reach any webpage as external communication. Please, take a big thanks from me. (Without your guys help I can't do a good job) . Thank you from Sydney_Australi a

                  Comment

                  • minhtran
                    New Member
                    • Feb 2008
                    • 28

                    #10
                    Originally posted by minhtran
                    Thank you InsertAliens
                    I just back to work after weekend. Today is Mon 14/ Apr/ 2008 in Sydney_Australi a)
                    YOU ARE RIGHT. Our server asks username and password b4 to reach any webpage as external communication. So I will solve this problem as to get permission for my coding directly reach any webpage as external communication. Please, take a big thanks from me. (Without your guys help I can't do a good job) . Thank you from Sydney_Australi a
                    Hi InserAliens
                    Again. you know how to code as send username and password to Proxy Server required to reach web page (That means you code as programming automatically get web page without asking username password again, cause of your coding has username and password "Our server asks username and password for external communication") . Thank you InsertAliens. Or Please, anyone has this idea to share, Thanks a lot to all.

                    Comment

                    Working...