Forum Visual Studio

REST PostAsync fails when running from an installed application in iPhone

coolrjmcoolrjm INMember ✭✭✭

Hi,

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.

            `try
            {
                //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");
                    }
                    else
                    {
                        client = new HttpClient() { BaseAddress = new Uri("http://192.168.1.1/") };

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

                            }
                            else
                            {
                                Debug.WriteLine("Transaction IncidentTable error:" + result.ResultDetails[0].Result.ToString());
                            }
                        }
                    }
                }
            }
            catch(TaskCanceledException tx)
            {
                DependencyService.Get<ISaveAndLoad>().SaveLogAsync("AppLog.txt","tx:"+tx.Message.ToString());
            }
            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 https://elvisrodrigues.wordpress.com/2015/03/19/solucao-httpclient-xamarin-only-http-or-https-scheme-is-allowed/
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)
Runtime:
Mono 5.4.1.7 (2017-06/e66d9abbb27) (64-bit)
GTK+ 2.24.23 (Raleigh theme)
Package version: 504010007

NuGet
Version: 4.3.1.4445

.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

Xamarin.Mac
Version: 4.0.0.214 (Visual Studio Enterprise)

Xamarin.iOS
Version: 11.6.1.2 (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.

Thanks,

Rajesh.

Answers

  • ArindamSarkarArindamSarkar Member ✭✭

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

  • coolrjmcoolrjm INMember ✭✭✭

    Hi,

    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.

    Thanks,

Sign In or Register to comment.