public override void ReceivedResponse(NSUrlConnection connection, NSUrlResponse response) { try { responseMessage.RequestMessage.RequestUri = new Uri(response.Url.ToString()); //At least attempt to get the url right if we redirected var httpResponse = response as NSHttpUrlResponse; if (httpResponse != null) { stream = new ByteArrayListStream(); content = new CancellableStreamContent(stream, () => { isComplete = true; stream.SetException(new OperationCanceledException()); }); responseMessage.StatusCode = (HttpStatusCode)httpResponse.StatusCode; responseMessage.Content = content; foreach (var header in httpResponse.AllHeaderFields) { if (header.Key != null && header.Value != null) { responseMessage.Headers.TryAddWithoutValidation(header.Key.ToString(), header.Value.ToString()); responseMessage.Content.Headers.TryAddWithoutValidation(header.Key.ToString(), header.Value.ToString()); } } } } finally { waitEvent.Set(); } }
public override void FailedWithError(NSUrlConnection connection, NSError error) { if (stream != null) { stream.Complete(); } waitEvent.Set(); }
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { return Task.Run(() => { var headers = request.Headers as IEnumerable<KeyValuePair<string, IEnumerable<string>>>; using(var ms = new MemoryStream()) { if(request.Content != null) { var t = request.Content.CopyToAsync(ms); t.Wait(); headers = headers.Union(request.Content.Headers); } var rq = new NSMutableUrlRequest() { AllowsCellularAccess = true, Body = NSData.FromArray(ms.ToArray()), CachePolicy = NSUrlRequestCachePolicy.ReloadIgnoringCacheData, Headers = headers.Aggregate(new NSMutableDictionary(), (acc, x) => { acc.Add(new NSString(x.Key), new NSString(String.Join(getHeaderSeparator(x.Key), x.Value))); return acc; }), HttpMethod = request.Method.ToString().ToUpperInvariant(), Url = NSUrl.FromString(request.RequestUri.AbsoluteUri), }; NativeMessageConnectionDelegate connectionDelegate = new NativeMessageConnectionDelegate(request); var connection = new NSUrlConnection(rq, connectionDelegate, false); connection.Schedule(NSRunLoop.Main, NSRunLoop.NSDefaultRunLoopMode); connection.Start(); connectionDelegate.wait(); return connectionDelegate.Response; } }); }
public override void ReceivedData(NSUrlConnection connection, NSData data) { try { using (var dataStream = data.AsStream()) { byte[] bytes = new byte[dataStream.Length]; int offset = 0; int read = 0; do { read = dataStream.Read(bytes, offset, bytes.Length - offset); } while(read > 0); stream.AddByteArray(bytes); } } catch(Exception) { } }
public override void ReceivedResponse(NSUrlConnection connection, NSUrlResponse response) { throw new System.NotImplementedException (); }
public override void ReceivedData(NSUrlConnection connection, NSData data) { throw new System.NotImplementedException (); }
public override void ReceivedAuthenticationChallenge(NSUrlConnection connection, NSUrlAuthenticationChallenge challenge) { throw new System.NotImplementedException (); }
public override void FinishedLoading(NSUrlConnection connection) { if (stream != null) { stream.Complete(); } waitEvent.Set(); }