Forum Xamarin.Forms

Reading text file with HttpWebRequest / HttpWebResponse

BobClemensBobClemens USMember ✭✭

Trying to read the contents of a text file that resides on the Web.
The following code works fine.
However, if the file has an arbitrary extension other than .txt or .html, the GetResponse returns error 404, Not Found (even though the file exists.)
Is there any way I can read files with arbitrary extensions? For example, myFile.xyz.

`   string url = @"http://www.xxxx.com/xxxx/myfile.txt";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    using (TextReader reader = new StreamReader(response.GetResponseStream()))
    {
        string s = reader.ReadToEnd();
        Console.WriteLine(s);
    }

}

`

Best Answer

Answers

  • JohnHardmanJohnHardman GBUniversity admin

    @BobClemens - As per the post by @Amar_Bait above (it's probably the second point @Amar_Bait raised). As an aside, you might want to make one change - if your files are large, you might want to consider using ReadAsStreamAsync() rather than ReadAsStringAsync().

Sign In or Register to comment.