Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to copy all text from a certain webpage and save it to notepad C#

I have a C# Windows Forms app that launches a webpage based on some criteria.

Now I would like my app to automatically copy all the text from that page (which is in CSV format) and paste and save it in notepad.

Here is a link to an example of the data that needs to be copied: http://www.wunderground.com/history/airport/FAJS/2012/10/28/DailyHistory.html?req_city=Johannesburg&req_state=&req_statename=South+Africa&format=1

Any Help will be appreciated.

like image 787
Patrick Doyle Avatar asked Oct 29 '12 09:10

Patrick Doyle


4 Answers

You can use the new toy HttpClient from .NET 4.5, example how to get google page:

 var httpClient = new HttpClient();
 File.WriteAllText("C:\\google.txt",    
                           httpClient.GetStringAsync("http://www.google.com")
                                     .Result);  
like image 140
cuongle Avatar answered Oct 19 '22 23:10

cuongle


http://msdn.microsoft.com/en-us/library/fhd1f0sw.aspx combined with http://www.dotnetspider.com/resources/21720-Writing-string-content-file.aspx

public static void DownloadString ()
{
    WebClient client = new WebClient();
    string reply = client.DownloadString("http://www.wunderground.com/history/airport/FAJS/2012/10/28/DailyHistory.html?req_city=Johannesburg&req_state=&req_statename=South+Africa&format=1");

    StringBuilder stringData = new StringBuilder();
    stringData = reply;  
    FileStream fs = new FileStream(@"C:\Temp\tmp.txt", FileMode.Create);
    byte[] buffer = new byte[stringData.Length];
    for (int i = 0; i < stringData.Length; i++)
    {
        buffer[i] = (byte)stringData[i];
    }
    fs.Write(buffer, 0, buffer.Length);
    fs.Close();
}

Edit Adil uses the WriteAllText method, which is even better. So you will get something like this:

WebClient client = new WebClient();
string reply = client.DownloadString("http://www.wunderground.com/history/airport/FAJS/2012/10/28/DailyHistory.html?req_city=Johannesburg&req_state=&req_statename=South+Africa&format=1");
System.IO.File.WriteAllText (@"C:\Temp\tmp.txt", reply);
like image 36
JP Hellemons Avatar answered Oct 19 '22 23:10

JP Hellemons


Simple way: use WebClient.DownloadFile and save as a .txt file:

var webClient = new WebClient();
webClient.DownloadFile("http://www.google.com",@"c:\google.txt");
like image 20
Ria Avatar answered Oct 20 '22 01:10

Ria


You need WebRequest to read the stream of and save to string to text file. You can use File.WriteAllText to write it to file.

WebRequest request = WebRequest.Create ("http://www.contoso.com/default.html");
                    request.Credentials = CredentialCache.DefaultCredentials;            
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();            
Console.WriteLine (response.StatusDescription);            
Stream dataStream = response.GetResponseStream ();            
StreamReader reader = new StreamReader (dataStream);            
string responseFromServer = reader.ReadToEnd ();
System.IO.File.WriteAllText (@"D:\path.txt", responseFromServer );
like image 27
Adil Avatar answered Oct 19 '22 23:10

Adil