Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Accessing text file with URL

Accessing text file with URL

Scheduled Pinned Locked Moved ASP.NET
comsysadminalgorithmsxmltutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Talal Sultan
    wrote on last edited by
    #1

    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 that TextReader or StreamReader 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

    N 1 Reply Last reply
    0
    • T Talal Sultan

      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 that TextReader or StreamReader 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

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      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 URL

      System.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


      My Website | Ask smart questions

      T 1 Reply Last reply
      0
      • N N a v a n e e t h

        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 URL

        System.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


        My Website | Ask smart questions

        T Offline
        T Offline
        Talal Sultan
        wrote on last edited by
        #3

        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

        N 1 Reply Last reply
        0
        • T Talal Sultan

          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

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          Thanks, I was thinking the second one too, but thought first will be good. Anyway nice comparison.


          My Website | Ask smart questions

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups