例如,当单击 UWP 中的按钮时,我需要从https://www.cbr-xml-daily.ru/daily_json.js获取 json。在控制台应用程序中,我可以这样做,例如,使用 WebClient:
using (WebClient wc = new WebClient())
{
Uri uri = new Uri("https://www.cbr-xml-daily.ru/daily_json.js");
var json = wc.DownloadString(uri);
Console.WriteLine(json);
}
或网络请求:
WebRequest request = WebRequest.Create("https://www.cbr-xml-daily.ru/daily_json.js");
WebResponse response = request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
string line = "";
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
response.Close();
Вызвано исключение: "System.Exception" в System.Private.CoreLib.dll
Сведения WinRT: Не удается найти сервер с таким именем или адресом
Вызвано исключение: "System.Exception" в System.Net.Http.dll
Сведения WinRT: Не удается найти сервер с таким именем или адресом
Вызвано исключение: "System.Exception" в System.Private.CoreLib.dll
Сведения WinRT: Не удается найти сервер с таким именем или адресом
"CurrencyConverter.exe" (CoreCLR: CoreCLR_UWP_Domain). Загружено "C:\Program Files\WindowsApps\Microsoft.NET.CoreFramework.Debug.2.2_2.2.29301.2_x64__8wekyb3d8bbwe\System.Resources.ResourceManager.dll".
Вызвано исключение: "System.Net.Http.HttpRequestException" в System.Net.Http.dll
Вызвано исключение: "System.Net.Http.HttpRequestException" в System.Private.CoreLib.dll
Вызвано исключение: "System.Net.Http.HttpRequestException" в System.Net.Http.dll
Вызвано исключение: "System.Net.Http.HttpRequestException" в System.Private.CoreLib.dll
Вызвано исключение: "System.Net.Http.HttpRequestException" в System.Private.CoreLib.dll

是的,应用程序无法访问 Internet,这一切都是因为 UWP。清单中给出了许可。