System.Net.Http.HttpRequestException:设备未配置 ---> System.Net.Sockets.SocketException:设备未配置
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
我在尝试从 aspnet 核心 Web 应用程序的自定义中间件发出 Web 请求时收到上述错误。错误发生在以下代码块的第四行:
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}");
我在 Mac 上使用 Visual Studio,社区版。是什么导致了这个错误?
更新
自最初的 post 以来,我尝试了一些方法但没有成功。这是我尝试过的:
- 运行 Visual Studio 中的应用程序,我电脑上的专业版 (Windows 10)
- 正在使用 HttpClient
尝试请求
- 在中间件之外尝试请求
我建议你使用 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();
确保正确键入请求 URI。
改变
string ipstackRaw = web.DownloadString($"http://api.ipstack/{ipaddress}");
到
string ipstackRaw = web.DownloadString($"http://api.ipstack.com/{ipaddress}");
System.Net.Http.HttpRequestException: Device not configured ---> System.Net.Sockets.SocketException: Device not configured
我在尝试从 aspnet 核心 Web 应用程序的自定义中间件发出 Web 请求时收到上述错误。错误发生在以下代码块的第四行:
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}");
我在 Mac 上使用 Visual Studio,社区版。是什么导致了这个错误?
更新
自最初的 post 以来,我尝试了一些方法但没有成功。这是我尝试过的:
- 运行 Visual Studio 中的应用程序,我电脑上的专业版 (Windows 10)
- 正在使用 HttpClient 尝试请求
- 在中间件之外尝试请求
我建议你使用 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();
确保正确键入请求 URI。
改变
string ipstackRaw = web.DownloadString($"http://api.ipstack/{ipaddress}");
到
string ipstackRaw = web.DownloadString($"http://api.ipstack.com/{ipaddress}");