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:
Make sure you type the request URI correctly.
Changestring ipstackRaw = web.DownloadString($"http://api.ipstack/{ipaddress}");
tostring ipstackRaw = web.DownloadString($"http://api.ipstack.com/{ipaddress}");
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With