我们正在xamarin ios本机应用程序中的聊天应用程序上工作,我们希望使用HttpClient PostAsync()方法将图像上传到服务器,但是它的抛出异常类似
System.Net.Http.HttpRequestException:发送请求时发生错误---> System.Net.WebException:错误:ConnectFailure(无路由到主机)---> System.Net.Sockets.SocketException:否路由到主机...。
选择图片后要上传的代码。.
public async void UploadToServer(string filePath, string filename, string fileExtension,long fileSize)
{
using(var client = new HttpClient())
using(var content= new MultipartFormDataContent())
{
client.BaseAddress = new Uri("http://*////");
var fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(filePath));
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = filePath
};
content.Add(fileContent);
var result = await client.PostAsync("api/FileUpload?groupID =" + ChatDetaiils.groupID + "&memberID=" + UserLogin.userID + "&Message=File Test&chatType=File&FileName="+filename+"&fileSize="+fileSize+"&fileExtension="+fileExtension+"",content);
}
}
我们在xamarin android本机应用程序中使用了相同的代码,并且在android中运行良好。 我们不知道我的错误是什么。我们正在使用Visual Studio for Mac。
请帮助解决此问题或告诉我另一种上传方法...
答案 0 :(得分:0)
我认为您可能会得到更好的结果:
[FunctionName("SendToOfflineArchive")]
public static void Run([TimerTrigger("%TimerInterval%")]TimerInfo myTimer, TraceWriter log)
{
log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
// do stuff
int nbErrors = 7;
//return nbErrors;
}
答案 1 :(得分:0)
我已经通过在基址中放置正确的uri解决了它。
client.BaseAddress = new Uri("http:***.***.*.**");