C# 错误:连接被拒绝。将文件上载到Asp Web Api
我正在使用Xamarin表单开发跨平台应用程序。我想选择一个图像并上传到ASP Web Api的文件夹。我可以选择图像,但我得到 未处理的异常:System.Net.WebException:错误:ConnectFailure(连接被拒绝) 当我试图上传时。我正在使用VS Android Emulator测试应用程序C# 错误:连接被拒绝。将文件上载到Asp Web Api,c#,xamarin.forms,httpclient,C#,Xamarin.forms,Httpclient,我正在使用Xamarin表单开发跨平台应用程序。我想选择一个图像并上传到ASP Web Api的文件夹。我可以选择图像,但我得到 未处理的异常:System.Net.WebException:错误:ConnectFailure(连接被拒绝) 当我试图上传时。我正在使用VS Android Emulator测试应用程序 private async void UploadFile_Clicked(object sender, EventArgs e) { var content = new
private async void UploadFile_Clicked(object sender, EventArgs e)
{
var content = new MultipartFormDataContent();
content.Add(new StreamContent(_mediaFile.GetStream()),
"\"file\"",
$"\"{_mediaFile.Path}\"");
var httpClient = new HttpClient();
var uploadServiceBaseAddress = "http://localhost:55678/api/Files/Upload";
var httpResponseMessage = await httpClient.PostAsync(uploadServiceBaseAddress, content);
RemotePathLabel.Text = await httpResponseMessage.Content.ReadAsStringAsync();
}
我插入了断点以查看异常,它发生在
var httpResponseMessage = await httpClient.PostAsync(uploadServiceBaseAddress, content);
我的代码中到底有什么问题?正如Jason所说,您正在尝试从localhost加载文件。在这种情况下,这是一个模拟器的地址,它没有意义,因为您的文件不在那里(模拟器和PC有不同的IP地址)。确保仿真器在与PC相同的网络上分配了IP地址,并使用PC地址加载文件能否获取内部异常/聚合异常详细信息?请尝试使用IP或FQDN,而不是本地主机