Cache And Compare Files In C#
Ok, I'm trying to make an application for an online Radio Station.
I have it set to read the song title and artist开发者_运维技巧 and write it to a text file on the webserver.
I want to have the application store the text in a string or a cache, and then reread it every 15 seconds and if it isn't the same then update the info box.
Where the text is stored: http://xcastradio.com/stats/nowplaying.txt
I don't need it coded for me. I would just like to know how to store text in a string from a website.
See the example for System.Net.WebRequest
.
Pulled from those docs (and modified for your applcation):
public String GetData(String url) {
WebRequest request = WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
String data = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
return data;
}
Call it as:
String data = GetData("http://xcastradio.com/stats/nowplaying.txt");
Use an HttpWebRequest
/HttpWebResponse
, use GetResponseStream
, read it until no more bytes can be read, and put that into a byte array.
After you have the string, you then open a FileStream
to a local file, and write that byte array with the Write
method.
精彩评论