Forum Visual Studio

REST PostAsync fails when running from an installed application in iPhone

coolrjmcoolrjm INMember ✭✭✭


I am facing a strange issue with the HttpClient in Xamarin forms when running the application from an iPhone.

I am uploading data from SQLite database on the mobile device to SQL Server via REST.

Code is as below.

                //we convert the C# object to a JSON object
                if (incident.Count > 0)
                    jsonjobhead = JsonConvert.SerializeObject(incident);

                    jsonjobhead = string.Concat("{", "\"", stringtopass1, "\":", jsonjobhead, "}");

                    //we push this JSON object to the URL            

                    if (!CrossConnectivity.Current.IsConnected)
                        bool isConnected = DependencyService.Get<ISignature>().AlertRest("Please Connect to a Network");
                        client = new HttpClient() { BaseAddress = new Uri("") };

                        client.Timeout = TimeSpan.FromSeconds(100000);

                        var content = new StringContent(jsonjobhead, Encoding.UTF8, "application/json");

                        HttpResponseMessage response = await client.PostAsync("InsertUpdateDatatoTransctions/", content);

                        var result = JsonConvert.DeserializeObject<YourClass>(response.Content.ReadAsStringAsync().Result);

                        if (result != null)
                            if (result.ResultDetails[0].Result.ToString() == "success")
                                flagsuccess = result.ResultDetails[0].Result.ToString();

                                Debug.WriteLine("Transaction IncidentTable error:" + result.ResultDetails[0].Result.ToString());
            catch(TaskCanceledException tx)
            catch(Exception ex)
                DependencyService.Get<ISaveAndLoad>().SaveLogAsync("AppLog.txt", "ex:" + ex.Message.ToString());

When I executed this code from Visual Studio during debug it works fine and the REST operation executed without any issue.

But when compiling the code to an iPA file to be installed in the iPhone, i am getting this error

ex:The request timed out.

I even followed the instructions as outlined here
because I was getting this error "Only http or https scheme is allowed".

Can anyone guide how to fix this issue of connecting to a REST Server from the application installed on the iPhone. I have read that this is a major bug in Xamarin.iOS when running from iPhone.

My version of Visual Studio along with Xamarin Forms and Xamarin.iOS are

Microsoft .Net.Http - 2.2.29

Visual Studio Enterprise 2017 for Mac
Version 7.3.2 (build 12)
Mono (2017-06/e66d9abbb27) (64-bit)
GTK+ 2.24.23 (Raleigh theme)
Package version: 504010007


.NET Core
Runtime: Not installed
SDK: Not installed
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.4.1/lib/mono/msbuild/15.0/bin/Sdks

Apple Developer Tools
Xcode 8.3.3 (12175.1)
Build 8E3004b

Version: (Visual Studio Enterprise)

Version: (Visual Studio Enterprise)
Branch: xcode9.2
Build date: 2017-12-04 21:20:41-0500

Build Information
Release ID: 703020012
Git revision: 7d0c9d7ad46b125cf4b7bbf9532c0531b6433ce6
Build date: 2017-12-12 14:03:18-05
Xamarin addins: 6dddaa49fbecb2695fcdcd6b712eb269e288518c
Build lane: monodevelop-lion-d15-5

Operating System
Mac OS X 10.12.6
Darwin 16.7.0 Darwin Kernel Version 16.7.0

Any hints to fix this problem will be appreciated.




  • ArindamSarkarArindamSarkar Member ✭✭

    Have you allowed HTTP transaction in info.plist in IOS. Like <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>

  • coolrjmcoolrjm INMember ✭✭✭


    Yes everything has been set. Why is that it fails when running from a iPA version but working fine when launching from Visual Studio and debugging it.


Sign In or Register to comment.