Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

How to solve HttpRequestException while calling HttpClient PostAsync method to upload image.

$
0
0

Hi,
We are working on a chat app in xamarin ios native app and we want upload a image to server using HttpClient PostAsync() method, but its throw exception like

"System.Net.Http.HttpRequestException: An error occurred while sending the request ---> System.Net.WebException: Error: ConnectFailure (No route to host) ---> System.Net.Sockets.SocketException: No route to host....".

Our code to upload after select a image is..

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);



                    }

            }

We are using same code in our xamarin android native app and its working good in android.
We don't know what was my mistake. We are using visual studio for mac.

Please help to solve this or tell me another method to upload...


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>