Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 259
Corey P Avatar asked Feb 16 '19 19:02

Corey P


2 Answers

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}");

like image 64
Corey P Avatar answered Oct 05 '22 23:10

Corey P


I recommend you to do it with HttpWebRequest

   string apiKey = "YOUR_ACCESS_KEY";
        var request = (HttpWebRequest)WebRequest.Create("https://api.ipstack.com/134.201.250.155?access_key="+ apiKey);
        request.Method = "GET";
        request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36";
        request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
        request.Headers.Add("accept-language", "en,hr;q=0.9");
        request.Headers.Add("accept-encoding", "");
        request.Headers.Add("Upgrade-Insecure-Requests", "1");
        WebResponse response = request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
        string responseFromServer = reader.ReadToEnd();
        reader.Close();
        response.Close();
like image 40
Demir Karic Avatar answered Oct 06 '22 00:10

Demir Karic