5

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.

Spontifixus
  • 6,570
  • 9
  • 45
  • 63
Patrick Doyle
  • 87
  • 2
  • 9

5 Answers5

4

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);  
cuongle
  • 74,024
  • 28
  • 151
  • 206
3

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);
JP Hellemons
  • 5,977
  • 11
  • 63
  • 128
3

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

var webClient = new WebClient();
webClient.DownloadFile("http://www.google.com",@"c:\google.txt");
Ria
  • 10,237
  • 3
  • 33
  • 60
1

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 );
Adil
  • 146,340
  • 25
  • 209
  • 204
0

You may use a webclient to do this:

System.Net.WebClient wc = new System.Net.WebClient();
byte[] raw = wc.DownloadData("http://www.wunderground.com/history/airport/FAJS/2012/10/28/DailyHistory.html?req_city=Johannesburg&req_state=&req_statename=South+Africa&format=1");

string webData = System.Text.Encoding.UTF8.GetString(raw);

then the string webData contains the complete text of the webpage

LostPhysx
  • 3,573
  • 8
  • 43
  • 73