Snake Eyes
Snake Eyes

Reputation: 16764

The request was aborted: The connection was closed unexpectedly in C# with WebClient

I have an issue getting html code from playstation page with WebClient and C#:

using (WebClient webCl = new WebClient())
{
    var url = "https://www.playstation.com/en-us/games/nioh-ps4/";

    var homeHtml = Encoding.UTF8.GetString(webCl.DownloadData(url));
    if (string.IsNullOrWhiteSpace(homeHtml))
    {
        return ...;
    }
    return ...;
}

On the line: Encoding.UTF8.GetString(webCl.DownloadData(url));

I got error:

The request was aborted: The connection was closed unexpectedly

Why ? What I'm doing wrong ?

Upvotes: 1

Views: 1401

Answers (2)

Pavan
Pavan

Reputation: 337

This is Due to URL access issue. It can occur due to multiple scenarios

  1. HOST is not accessible from your machine.
  2. Host is rejecting the request.
  3. Firewall blocking the request

Try ping www.playstation.com. you should get the response.

Upvotes: 0

Sentinel
Sentinel

Reputation: 3697

One possibility is that your firewall is not allowing your app through. Adjust your security settings to allow the app through and try again.

Another possibility is that the server is throttling your connection, if you are crawling or hitting it too fast. Verify your code by running against other servers (eg: google.com) with a very limited rate.

Other possibilities include that there is something else the server does not like. For example, you may need to add a user agent header:

request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";

Or you may need to play with cookies.

You can use Fiddler to compare differences between browser and app requests and adjust accordingly.

Upvotes: 1

Related Questions