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

sumitmishrasumitmishra Member ✭✭

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...

Tagged:

Posts

  • sumitmishrasumitmishra Member ✭✭
    edited July 2018

    GetStringAsync() method working with http: but PostAsync() throw exception mention above.
    I have also added

    NSAppTransportSecurity

    NSAllowsArbitraryLoads

    in info.plist but its not working

    Please guide..

  • RomDevRomDev Member ✭✭

    Same issue. Managed to get past it by replacing localhost with my local ipv4 address. That is the only way I made post working in debug mode with HttpClient on Forms.

  • sumitmishrasumitmishra Member ✭✭

    Hi @RomDev ,
    Check your base address properly, We had written wrong address and solve it by putting correct address.

    client.BaseAddress = new Uri("http://*////");

Sign In or Register to comment.