Reading Remote File [C #]

I am trying to read a remote file using HttpWebRequest in a C # console application. But for some reason the request is empty - it never finds the url.

This is my code:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://uo.neverlandsreborn.org:8000/botticus/status.ecl");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

      

Why is it impossible?

The file contains only a line. Nothing more!

+2


source to share


2 answers


How do you read the response data? Does it return as successful but empty, or is there an error status?

If that doesn't work, try Wireshark , which will let you see what's going on at the network level.



Also, consider using WebClient

instead WebRequest

- it makes it incredibly easy when you don't have to do anything fancy:

string url = "http://uo.neverlandsreborn.org:8000/botticus/status.ecl";
WebClient wc = new WebClient();
string data = wc.DownloadString(url);

      

+12


source


You should receive a stream of responses and read data from that. Here's a function I wrote for one project that does exactly that:



    private static string GetUrl(string url)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            if (response.StatusCode != HttpStatusCode.OK)
                throw new ServerException("Server returned an error code (" + ((int)response.StatusCode).ToString() +
                    ") while trying to retrieve a new key: " + response.StatusDescription);

            using (var sr = new StreamReader(response.GetResponseStream()))
            {
                return sr.ReadToEnd();
            }
        }
    }

      

+3


source







All Articles