question

mo-3013 avatar image
0 Votes"
mo-3013 asked WenyanZhang-MSFT commented

HttpClient Response Timeout

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.


dotnet-csharpdotnet-xamarin
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It turns out its only the post method not receiving a response, but a get method can receive just fine. Is this some kind of firewall issue? I am testing on ios devices and set AllowArbitraryLoad to be true in info.plist.

0 Votes 0 ·
WenyanZhang-MSFT avatar image
1 Vote"
WenyanZhang-MSFT answered WenyanZhang-MSFT commented

Hello @mo-3013,

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://docs.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://docs.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.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, Zhang thanks for looking into my issue.
There is one more concern I want to ask about after not finding any similar issue on the web.
Is there a size limit when using httpclient to send files with post request? If I use a string less than 500,000 in length as content in the HttpRequest. the request will still hang after 90 seconds.
However, if I use 100,000 character-long string the response can be fetched in less than 1 second, though on the server side both give a 200 OK response.
My server is an amazon free tier ec2 instance running flask, is it generally not a good idea to send a 20mbytes-ish with this kind of server because of some limitation?

Thanks in advance

0 Votes 0 ·

Try to set an infinite timeout for testing if it is caused by the content-length, refer to https://docs.microsoft.com/en-us/dotnet/api/system.threading.timeout.infinitetimespan?view=net-6.0
Usually, if you didn't set the content-length in HTTP headers, there is no limitation. As far as I know, the Tomcat default limitation is 2M. But I'm not clear about your server side, and I'm not proficient with server. You could connect with the server support for help or feel free to post new thread for the question.

0 Votes 0 ·
AgaveJoe avatar image
0 Votes"
AgaveJoe answered mo-3013 edited

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()?

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi AgaveJose, thanks for replying.
Yes there is a POST method handler in the Flask script. Messed around a little bit yesterday and successfully receive a post response without Content or with an empty string as content in the request.
It is likely that didn't encode the png right into json. But since the server gives 200 OK everytime a post request is sent I presume it is because the image is too big.
The GetInstance is returning a singleton instance of class Doctor defined in DoctorModel.cs. It is initialized as soon as the app is started and a HttpClient is created inside.
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?

0 Votes 0 ·
AgaveJoe avatar image
1 Vote"
AgaveJoe answered mo-3013 edited

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


This is my encoding process at the beginning of Examine

                 var ms = new MemoryStream();
                 imageStream.CopyTo(ms);
                 byte[] imageBytes = ms.ToArray();
                 String i = Convert.ToBase64String(imageBytes);
                 //demo id
                 Scan scan = new Scan() { ID = "12345678910", date = "1970-01-01 10:00:00", image = i };//another class object in DoctorModel.cs
                 string jsonScan = JsonConvert.SerializeObject(scan);

So you are suggesting I should not use base64 to encode when I am preparing http post request?
I am also very new to http protocol


0 Votes 0 ·