2

Why does this not works -

"The underlying connection was closed: The connection was closed unexpectedly"

    static void Main()
    {
        using (var client = new WebClient())
        {
            try
            {
                Console.WriteLine(client.DownloadString("http://oz.by/books/more10176026.html"));
            }
            catch (Exception e)
            {

                throw;
            }
        }
    }

Usual GET requests are OK. check it here

1
  • Can you use a web debugger like Fiddler and check what's going on with the request? That would probably give a hint. Commented Dec 23, 2013 at 18:50

1 Answer 1

3

The server seems to choke because there is no User Agent header. This fixes it:

using (var client = new WebClient())
{
    try
    {
        //Add your user agent of choice. This is mine, just as an example.
        client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.73.11 (KHTML, like Gecko) Version/7.0.1 Safari/537.73.11");
        Console.WriteLine(client.DownloadString("http://oz.by/books/more10176026.html"));
    }
    catch (Exception e)
    {

        throw;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

yes, this works. I have followed msdn, and this was my mistake)) msdn.microsoft.com/en-us/library/fhd1f0sw(v=vs.110).aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.