Search code examples
c#webclientdownloadstring

The underlying connection was closed exception while WebClient DownloadString


Just a piece of code

WebClient wc = new WebClient();
String str = wc.DownloadString(new Uri("http://content.warframe.com/dynamic/rss.php"));

And I got exception:

An unhandled exception of type 'System.Net.WebException' occurred in System.dll

Additional information: The underlying connection was closed: The connection was closed unexpectedly.

I've head that this is a bug in .NET (I am using 3.5), but I tried other methods to obtain the content of this link (its rss, xml). No lucky shot yet

var webrequest = (WebRequest)HttpWebRequest.Create(@"http://content.warframe.com/dynamic/rss.php");
var resp = webrequest.GetResponse();
//HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse(); // Wont work also

This code above won't work either, both casts the same exception

Fiddler logs:

SESSION STATE: Aborted.
Response Entity Size: 512 bytes.

== FLAGS ==================
BitFlags: [ResponseGeneratedByFiddler] 0x100
X-ABORTED-WHEN: Done
X-CLIENTIP: 127.0.0.1
X-CLIENTPORT: 2747
X-EGRESSPORT: 2748
X-FAILSESSION-WHEN: ReadingResponse
X-HOSTIP: 205.185.216.10
X-PROCESSINFO: willitwork.vshost:3300

== TIMING INFO ============
ClientConnected: 10:29:11.706
ClientBeginRequest: 10:29:11.713
GotRequestHeaders: 10:29:11.713
ClientDoneRequest: 10:29:11.713
Determine Gateway: 0ms
DNS Lookup: 164ms
TCP/IP Connect: 74ms
HTTPS Handshake: 0ms
ServerConnected: 10:29:11.953
FiddlerBeginRequest: 10:29:11.953
ServerGotRequest: 10:29:11.953
ServerBeginResponse: 10:29:12.372
GotResponseHeaders: 00:00:00.000
ServerDoneResponse: 10:29:12.372
ClientBeginResponse: 10:29:12.385
ClientDoneResponse: 10:29:12.385

Overall Elapsed: 0:00:00.672

The response was buffered before delivery to the client.

== WININET CACHE INFO ============
This URL is not present in the WinINET cache. [Code: 2]
* Note: Data above shows WinINET's current cache state, not the state at the time of the request.
* Note: Data above shows WinINET's Medium Integrity (non-Protected Mode) cache only.

Also - 504, this does not makes sense since I can get data from link via chrome / firefox / ie...

I just did it to work in other language, but I am forced to do it with C# (I' ve made 2 much code to rewrite it)

I've added some settings like fiddler said

myHttpWebRequest1.ProtocolVersion = HttpVersion.Version11;
myHttpWebRequest1.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36";
myHttpWebRequest1.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";

At least now I get 504 error instead of "unknown", but I can still view the content via webbrowser, so the 504 error is fake

Edit: There is no response error when I added

myHttpWebRequest1.Headers["Accept-Encoding"] = "gzip";

but now the output is messed and unreadable


Solution

  • Ok, i got this all fixes & working!

    static void Main(string[] args)
    {
        Uri url = new Uri(@"http://content.warframe.com/dynamic/rss.php");
    
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    
        // MAGIC LINE GOES HERE \/
        request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
    
        // Assign the response object of HttpWebRequest to a HttpWebResponse variable.
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            using (Stream streamResponse = response.GetResponseStream())
            {
                using (StreamReader streamRead = new StreamReader(streamResponse))
                {
                    Char[] readBuff = new Char[2000];
                    int count = streamRead.Read(readBuff, 0, 2000);
                    while (count > 0)
                    {
                        String outputData = new String(readBuff, 0, count);
                        Console.Write(outputData);
                        count = streamRead.Read(readBuff, 0, 2000);
                    }
                }
            }
        }
    
        Console.ReadKey();
    }
    

    Besides of not-using WebClient.DownloadString method i had to add decompresion line

    request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
    

    Thanks for tips (especially fiddler one, Decode button saved my time to find what's wrong)