我正在使用UWP C#开发一个应用,其中一个功能是可以通过网络接口下载安装包,我使用HttpClient类进行Get请求,但是在GetAsync接口被调用时抛出异常,内容为:无法与服务器建立连接,我已在appxmanifest文件中声明internetClient功能,请问该异常是由什么原因导致,应该如何解决
高杰 王
0
信誉分
string url = $"{baseUrl}?model={param}";
using (HttpClient client = new HttpClient())
{
try
{
using (HttpResponseMessage response = await client.GetAsync(new Uri(url)))
{
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
string newData = await Task.Run(() =>
{
ResponseData responseData = JsonConvert.DeserializeObject<ResponseData>(responseBody);
return responseData.data.Replace("data:application/x-msdownload;base64,", "");
});
/*Debug.WriteLine("responseBody: " + responseData.data);*/
string savePath = await SaveBase64ToFileAsync(newData, param + ".exe");
if (!savePath.Equals(null))
{
path = savePath;
}
}
else
{
await logger.LogMessageAsync($"HttpClient download request failure: {response.StatusCode}");
}
}
}
catch (Exception ex)
{
await logger.LogMessageAsync($"download Driver have an Error: {ex.Message} {ex.InnerException}");
}
}
代码如上,应如何解决该问题
登录以回答