Share via

HttpClient Response Timeout

某も mo 21 Reputation points
2022-02-26T22:09:17.733+00:00

Hello, I have been having trouble debugging my xamarin.forms code not able to receive a valid post response from an AWS server with Flask running
Here are the snippets in my xamarin app:
<---PhotoViewModel.cs---->

ExaminePhotoCommand = new Command(async () => await ExaminePhoto()); // binded to a button because of MVVM pattern
.

.
   async Task ExaminePhoto()
        {

            if (photo == null) //path to photo
            {
                Console.Write("Has not taken a photo");
                return;
            }
            Stream photoStream = await photo.OpenReadAsync();
            await Doctor.GetInstance().Examine(photoStream); //calling Examine in DoctorModel

        }

<--->

<--DoctorModel.cs--->

using Newtonsoft.Json;
using System.Net.Http;


    public async Task Examine(Stream imageStream)
            {
                <--Image to Json Conversion->

                try
                {

                    HttpRequestMessage request = new HttpRequestMessage
                    {
                        Method = HttpMethod.Post,
                        RequestUri = new Uri("http//:url"),
                        Content = new StringContent(jsonScan)
                    };
                    Console.WriteLine("Sending request");
                    HttpResponseMessage response = await client.SendAsync(request);  // the response will always timeout after client's Timespan
                    string result = await response.Content.ReadAsStringAsync();
                    Console.WriteLine(result);
                }
                catch (Exception e)
                {

                    Console.WriteLine("Examine timeout");
                }

            }

<----->

I am a beginner in xamarin forms and c# concurrency concepts.
The server gives 200 status every time the request is sent but a response is never received for some reason.
I am not sure if this is caused by my code or some setting in the server.

Developer technologies | .NET | Xamarin
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


Answer accepted by question author

Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,456 Reputation points Microsoft External Staff
2022-03-01T05:44:22.2+00:00

Hello @某も mo ,

I think the real question is how to send a jsonencoded picture quickly without it being time out given that the client does not wait more than 10 seconds

The 10-second timeout is a little short, you could try to extend the timeout, refer to:

HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(90) }; // such as 90s  

You also need to check the timeout of your server.

In addition, you said "the image is too big", consider to compress the image while uploading, refer to https://learn.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/xamformsimageresize/

If the image is too large to affect the main thread, consider to make a background session and upload task on iOS, refer to https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-walkthroughs/background-transfer-walkthrough

Best Regards,
Wenyan Zhang


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. AgaveJoe 31,361 Reputation points
    2022-02-28T18:53:39.237+00:00

    Now I think the real question is how to send a jsonencoded picture quickly without it being time out given that the client does not wait more than 10 seconds?

    I'm not familiar with a json encoded image. Usually images are Base64 encoded when not using a standard HTTP multipart/form-data. Base64 encoding increases the image size by 1/3.

    Was this answer helpful?

    1 person found this answer helpful.

  2. AgaveJoe 31,361 Reputation points
    2022-02-27T13:20:37.703+00:00

    The first thing I would look at is the AWS services. Is there a POST method? Can you execute the post method with another client like Postman?

    Next, troubleshoot the client. What is the value of jsonScan? Is it a valid JSON string? Is the URL correct?

    Lastly, can you explain the design and the need for GetInstance()?

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.