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
9 3060
do u want to read web page of any site. It is possible through Net class in asp.net
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)
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: -
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: -
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;
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: -
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: -
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 = (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.
-
<%@ 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()
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?
<%@ 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.
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |