473,394 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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
Apr 10 '08 #1
9 3060
kunal pawar
297 100+
do u want to read web page of any site. It is possible through Net class in asp.net
Apr 10 '08 #2
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)
Apr 11 '08 #3
Curtis Rutland
3,256 Expert 2GB
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.WriteLine(). Add theses lines to the end:

Expand|Select|Wrap|Line Numbers
  1. FileStream fs = new FileStream(@"c:\google.txt",FileMode.OpenOrCreate,FileAccess.Write);
  2. StreamWriter sw = new StreamWriter(fs);
  3. sw.Write(sb.ToString());
  4. sw.Close();
  5. fs.Close();
  6.  
Replace "google.txt" with the path of your choice.

Now, as to using it later through a web request, use this:
Expand|Select|Wrap|Line Numbers
  1. FileStream fs = new FileStream(@"c:\google.txt", FileMode.Open, FileAccess.Read);
  2. StreamReader sw = new StreamReader(fs);
  3. string buffer = sw.ReadToEnd();
  4. Response.Write(buffer);
  5. Response.End();
  6.  
Replacing "google.txt" again with the path to the text file, Remember to include:
using System.IO;
Apr 11 '08 #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.WriteLine(). Add theses lines to the end:

Expand|Select|Wrap|Line Numbers
  1. FileStream fs = new FileStream(@"c:\google.txt",FileMode.OpenOrCreate,FileAccess.Write);
  2. StreamWriter sw = new StreamWriter(fs);
  3. sw.Write(sb.ToString());
  4. sw.Close();
  5. fs.Close();
  6.  
Replace "google.txt" with the path of your choice.

Now, as to using it later through a web request, use this:
Expand|Select|Wrap|Line Numbers
  1. FileStream fs = new FileStream(@"c:\google.txt", FileMode.Open, FileAccess.Read);
  2. StreamReader sw = new StreamReader(fs);
  3. string buffer = sw.ReadToEnd();
  4. Response.Write(buffer);
  5. Response.End();
  6.  
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 = (HttpWebResponse)
request.GetResponse();
(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.business.gov.au"). Thank you very much for your help and Ideas.
Apr 11 '08 #5
kunal pawar
297 100+
Expand|Select|Wrap|Line Numbers
  1. <%@ Import Namespace="system"  %>
  2. <%@ Import Namespace="system.io"  %>
  3. <%@ Import Namespace="system.Net"  %>
  4.  
  5.  
  6.  
  7.  
  8.         Dim webObj As New WebClient()
  9.  
  10.         Dim str As String = ""
  11.         Dim byt As Byte()
  12.         Dim strSearch As String = ""
  13.         Dim strOp As String = ""
  14.         Dim uft8Obj As New UTF8Encoding
  15.  
  16.  
  17.             Dim strUrl As String = "http://expasy.org/cgi-bin/pi_tool1?"
  18.             byt = webObj.DownloadData(strUrl)
  19.             strSearch = uft8Obj.GetString(byt)
  20.  
  21.             webObj.Dispose()
  22.  
following code to write text file
Expand|Select|Wrap|Line Numbers
  1. 'Open a file for writing
  2.     Dim FILENAME as String = Server.MapPath("Output.txt")
  3.  
  4.     'Get a StreamReader class that can be used to read the file
  5.     Dim objStreamWriter as StreamWriter
  6.     objStreamWriter = File.AppendText(FILENAME)
  7.  
  8.     'Append the the end of the string, "A user viewed this demo at: "
  9.     'followed by the current date and time
  10.     objStreamWriter.WriteLine("A user viewed this demo at: " & DateTime.Now.ToString())
  11.  
  12.     'Close the stream
  13.     objStreamWriter.Close()
Apr 11 '08 #6
Curtis Rutland
3,256 Expert 2GB
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();
(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?
Apr 11 '08 #7
<%@ Import Namespace="system" %>
<%@ Import Namespace="system.io" %>
<%@ Import Namespace="system.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.DownloadData(strUrl)
strSearch = uft8Obj.GetString(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.ToString())

'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_Australia)
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.
Apr 14 '08 #8
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_Australia)
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_Australia
Apr 14 '08 #9
Thank you InsertAliens
I just back to work after weekend. Today is Mon 14/ Apr/ 2008 in Sydney_Australia)
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_Australia
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.
Apr 14 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Martin Dale | last post by:
Dear all, Is there any way to activate the Save As command from Javascript. I would like to have a "Save as" button on a page which has been dynamically created (client side using...
4
by: Nikhil Tayal | last post by:
Is there a way to write a file on the client machine from an aspx page? I've a custom query page and need to store the search criteria specified in an XML file on the user machine from my web page...
12
by: Daniel Walzenbach | last post by:
Hi, I have a Website which allows users to input data. After they finished entering data they can click a button to save their input. Problem now is, that I have no possibility to visualize that...
2
by: fmakopo | last post by:
I am trying to save html page using C# because i want to save the page on the server not on the client side. I can do that using Javascript the problem that i am having i want to save the page on...
5
by: hp_1981 | last post by:
Hi Is there anyway avoiding users to save my web pages? this idea came to my mind when I tried to save a web page, but in the middle of saving progress something like the following error...
3
by: fiefie.niles | last post by:
I would like to save a web page to a file and have the hyperlinks work when I bring the file back up. If the web page has a hyperlink like the following <a href="OurWeb/News/abcFile.htm">, after...
3
by: fiefie.niles | last post by:
I would like to save a web page to a file. When I do that, sometimes the saved web page does not look like the original web page. For example, when I save www.msn.com, it looks very different the...
12
by: =?Utf-8?B?RnJlZU5FYXN5?= | last post by:
Hello, the scenario: There's an ASPX page which shows some text and has three buttons at the bottom: Save, Print and Close. Print and close is done by javascript. But how can I save the page...
2
by: ppuniversal | last post by:
Hi All, My question is as follows: I have a text box and two links.Let the two links be - Show and Save . On clicking the Show , a web page is generated by fetching some data from database...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.