Accessing text file with URL
-
Hi people, I have a web service method that I would like to use to access a text file over the network, say the file is located at
http://someserver.com/somefolder/myfile.txt
. I have been searching with google and all I could find was how to read an XML file by providing a URL in this article: http://support.microsoft.com/kb/307643[^] I was wondering if there was a way to do the same but with text files instead of XML files. It seems thatTextReader
orStreamReader
do not access a URL as the path of the file. Any insights are appreciated. Thanks :)-- If this is a post that has been helpful to you, please vote for it. Thank you! "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook
-
Hi people, I have a web service method that I would like to use to access a text file over the network, say the file is located at
http://someserver.com/somefolder/myfile.txt
. I have been searching with google and all I could find was how to read an XML file by providing a URL in this article: http://support.microsoft.com/kb/307643[^] I was wondering if there was a way to do the same but with text files instead of XML files. It seems thatTextReader
orStreamReader
do not access a URL as the path of the file. Any insights are appreciated. Thanks :)-- If this is a post that has been helpful to you, please vote for it. Thank you! "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook
Talal Sultan wrote:
I was wondering if there was a way to do the same but with text files instead of XML files.
Talal, You can make use of
WebClient
class. This can be used to download data from remote URL. See the following snippet explains how you can read the text file from URLSystem.Net.WebClient wc = new System.Net.WebClient();
byte[] b = wc.DownloadData("http://someserver.com/somefolder/myfile.txt");
string Text = System.Text.ASCIIEncoding.ASCII.GetString(b);That's it, you will get text file content in Text variable. Now use some kind of regular expressions to parse it and read the necessary content. Hope this helps
-
Talal Sultan wrote:
I was wondering if there was a way to do the same but with text files instead of XML files.
Talal, You can make use of
WebClient
class. This can be used to download data from remote URL. See the following snippet explains how you can read the text file from URLSystem.Net.WebClient wc = new System.Net.WebClient();
byte[] b = wc.DownloadData("http://someserver.com/somefolder/myfile.txt");
string Text = System.Text.ASCIIEncoding.ASCII.GetString(b);That's it, you will get text file content in Text variable. Now use some kind of regular expressions to parse it and read the necessary content. Hope this helps
Hi, Thanks for the hint :) I found 2 ways to do the job. I will post them here maybe it would help others having the same problem. Using your code, I found that if we use the
DownloadString
, it works too. We can write:WebClient wc = new WebClient();
string s = wc.DownloadString("http://someserver.com/somefolder/myfile.txt");this will put all the text file in
s
. We will get of course \r\n each time there is a new line. Another method can be used which can allow us to read line by line:WebRequest wRequest = HttpWebRequest.Create("http://someserver.com/somefolder/myfile.txt");
WebResponse wResponse = wRequest.GetResponse();
Stream myStream = wResponse.GetResponseStream();
StreamReader sr = new StreamReader(myStream);
string firstline = sr.ReadLine();This is even better as we don't have to parse the string to remove the \r\n or other special characters. Voila. Thanks again for the hint :)
-- If this is a post that has been helpful to you, please vote for it. Thank you! "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook
-
Hi, Thanks for the hint :) I found 2 ways to do the job. I will post them here maybe it would help others having the same problem. Using your code, I found that if we use the
DownloadString
, it works too. We can write:WebClient wc = new WebClient();
string s = wc.DownloadString("http://someserver.com/somefolder/myfile.txt");this will put all the text file in
s
. We will get of course \r\n each time there is a new line. Another method can be used which can allow us to read line by line:WebRequest wRequest = HttpWebRequest.Create("http://someserver.com/somefolder/myfile.txt");
WebResponse wResponse = wRequest.GetResponse();
Stream myStream = wResponse.GetResponseStream();
StreamReader sr = new StreamReader(myStream);
string firstline = sr.ReadLine();This is even better as we don't have to parse the string to remove the \r\n or other special characters. Voila. Thanks again for the hint :)
-- If this is a post that has been helpful to you, please vote for it. Thank you! "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook
Thanks, I was thinking the second one too, but thought first will be good. Anyway nice comparison.