Forum Xamarin.iOS

watchOS Web API Call

SebastianKruseSebastianKruse USMember ✭✭✭
edited February 2017 in Xamarin.iOS

Is it possible to call a web REST API without the need of the running parent app? I tried it with this, but only end up with exceptions. Also I cannot find any resource on the web regarding this topic.

        private async Task<string> GetResponseAsync(string method, Dictionary<string, string> data)
        {
            using (var handler = new HttpClientHandler { CookieContainer = new CookieContainer() })
            {
                using (var client = new HttpClient(handler) { BaseAddress = new Uri("https://api.development-blog.eu/MyApp/") })
                {
                    var authentication = Convert.ToBase64String(Encoding.UTF8.GetBytes("myuser:mypassword"));

                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authentication);
                    var response = await client.PostAsync($"?method={method}", new FormUrlEncodedContent(data));

                    return await response.Content.ReadAsStringAsync();
                }
            }
        }

This is the exception I get, but I don't know exactly what it means:

** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -setDelegate: only defined for abstract class. Define -[System_Net_Http_NSUrlSessionHandler_WrappedNSInputStream setDelegate:]!'
*** First throw call stack:
(
0 CoreFoundation 0x00753d74 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x052c1df4 objc_exception_throw + 50
2 CoreFoundation 0x00753c9d +[NSException raise:format:] + 141
3 Foundation 0x00bfd18b NSRequestConcreteImplementation + 216
4 Foundation 0x00c0548b -[NSStream(NSStream) setDelegate:] + 50
5 CFNetwork 0x047bc508 -[__NSCFBackgroundDataTask _onqueue_captureStream:] + 369
6 CFNetwork 0x047bc758 __70-[__NSCFBackgroundDataTask _onqueue_needNewBodyStream:withCompletion:]_block_invoke_2 + 86
7 libdispatch.dylib 0x05dea71b _dispatch_call_block_and_release + 15
8 libdispatch.dylib 0x05e083fd _dispatch_client_callout + 14
9 libdispatch.dylib 0x05df0066 _dispatch_queue_drain + 1065
10 libdispatch.dylib 0x05def9b1 _dispatch_queue_invoke + 563
11 libdispatch.dylib 0x05df145a _dispatch_root_queue_drain + 442
12 libdispatch.dylib 0x05df1299 _dispatch_worker_thread3 + 108
13 libsystem_pthread.dylib 0x06130d3e _pthread_wqthread + 1070
14 libsystem_pthread.dylib 0x061308ea start_wqthread + 34
)
libc++abi.dylib: terminating with uncaught exception of type NSException

I hope that someone can help me soon. This is the last missing part before I can try to publish again.

Sign In or Register to comment.