0

So I'm trying to send a multiform POST to API with http client but it's just hang there indefinetly. I test this code in console and it worked as it should, but then I try to run it like this for the UI

private static async Task<string> ApiTask(...)
{
    var SourceStream = File.Open(imgpath,FileMode.Open);
    var FileStreamContent = new StreamContent(SourceStream);
    FileStreamContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
    var client = new HttpClient();
    using (var formData = new MultipartFormDataContent())
    {
        formData.Add(new StringContent("this is a test"),"comment");
        formData.Add(new StringContent("Command: detect"),"message");
        formData.Add(fileStreamContent, "image","image.jpg");
        var response = await client.PostAsync(url,formData).ConfigureAwait(false);
        var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
        return responseString
    }
}

And I'm calling it from the EventHandler

public async void buttnclck(object sender, EventArgs e)
{
    var task = await ApiTask(...);
    lblresult.Text = task;
}

but as I said the code just stay in de .PostAsync line indefinetly or when a System.Threading.Task.TaskCanceledException is thrown.

So what I missing here? I thing I was handeling the async/await methods just fine but it's clear I'm not. I tried also with .Result but it won't work even and would throw System.AggregateException. So please help, been trying to make it work modifying the code as other suggested responses but still not working

EDIT: after couple of hours debugging and searching I find out my problem relies in formData.Add(FileStreamContent, "image","image.jpg"); maybe I'm not serializing the image correctly? How can I fix this??

Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59
fase4
  • 1
  • 2
  • Make your life easier - https://stackoverflow.com/questions/15176538/net-httpclient-how-to-post-string-value – Sean Mitchell May 13 '18 at 11:21
  • Possible duplicate of [.NET HttpClient. How to POST string value?](https://stackoverflow.com/questions/15176538/net-httpclient-how-to-post-string-value) – Sean Mitchell May 13 '18 at 11:22
  • @SeanMitchell already tried that and didn't work – fase4 May 13 '18 at 11:40
  • Have you tried using Fiddler to see what goes on the wire? – Paulo Morgado May 13 '18 at 14:25
  • @PauloMorgado not fiddler but postman. I find out my problem was at sending the image to the server and I need to figure it out how to send it the right way – fase4 May 13 '18 at 15:36
  • How did you use Postman to see what the HttpClient was sending and receiving? – Paulo Morgado May 13 '18 at 18:46
  • @PauloMorgado I don't, emulator has been buggy lately and fiddler doesn't catch my andorid device by usb. I used postman just to check if the server where running fine and it did. I'm pretty sure my problem relies in not been able to sent the image on the body of the post – fase4 May 13 '18 at 23:09
  • So, this is, definitely, not a `visual-studio` problem. It might be a `xamarin android` problem. – Paulo Morgado May 14 '18 at 06:48

0 Answers0