Search code examples
c#visual-studioasp.net-corehttprequestmiddleware

System.Net.Http.HttpRequestException: Device not configured ---> System.Net.Sockets.SocketException: Device not configured


System.Net.Http.HttpRequestException: Device not configured ---> System.Net.Sockets.SocketException: Device not configured

I'm receiving the above error when trying to make a web request from a custom middleware piece for an aspnet core web application. The error occurs on the fourth line of the following block of code:

var web = new WebClient();
var testing = _configuration.GetSection("IPStack")["AccessKey"];
web.QueryString.Add("access_key", _configuration.GetSection("IPStack")["AccessKey"]);
string ipstackRaw = web.DownloadString($"http://api.ipstack/{ipaddress}");

I'm using Visual Studio on Mac, Community Edition. What's causing this error?

Update

I've tried a few things since the original post without success. Here is what I have tried:

  • Running the application in Visual Studio, Professional Edition on my PC (Windows 10)
  • Trying the request using HttpClient
  • Trying the request outside the middleware

Solution

  • Make sure you type the request URI correctly.

    Change
    string ipstackRaw = web.DownloadString($"http://api.ipstack/{ipaddress}");
    to
    string ipstackRaw = web.DownloadString($"http://api.ipstack.com/{ipaddress}");