Beispiel #1
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var headers = request.Headers as IEnumerable <KeyValuePair <string, IEnumerable <string> > >;
            var ms      = new MemoryStream();

            if (request.Content != null)
            {
                await request.Content.CopyToAsync(ms).ConfigureAwait(false);

                headers = headers.Union(request.Content.Headers).ToArray();
            }

            var rq = new NSMutableUrlRequest()
            {
                AllowsCellularAccess = true,
                Body        = NSData.FromArray(ms.ToArray()),
                CachePolicy = (!this.DisableCaching ? NSUrlRequestCachePolicy.UseProtocolCachePolicy : 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),
            };

            if (Timeout != null)
            {
                rq.TimeoutInterval = Timeout.Value.Seconds;
            }

            var op = session.CreateDataTask(rq);

            cancellationToken.ThrowIfCancellationRequested();

            var ret = new TaskCompletionSource <HttpResponseMessage>();

            cancellationToken.Register(() => ret.TrySetCanceled());

            lock (inflightRequests) {
                inflightRequests[op] = new InflightOperation()
                {
                    FutureResponse    = ret,
                    Request           = request,
                    Progress          = getAndRemoveCallbackFromRegister(request),
                    ResponseBody      = new ByteArrayListStream(),
                    CancellationToken = cancellationToken,
                };
            }

            op.Resume();
            return(await ret.Task.ConfigureAwait(false));
        }
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            setNSCookies();

            var headers = request.Headers as IEnumerable <KeyValuePair <string, IEnumerable <string> > >;
            var ms      = new MemoryStream();

            if (request.Content != null)
            {
                await request.Content.CopyToAsync(ms).ConfigureAwait(false);

                headers = headers.Union(request.Content.Headers).ToArray();
            }

            var rq = new NSMutableUrlRequest()
            {
                AllowsCellularAccess = true,
                Body        = NSData.FromArray(ms.ToArray()),
                CachePolicy = NSUrlRequestCachePolicy.UseProtocolCachePolicy,
                Headers     = headers.Aggregate(new NSMutableDictionary(), (acc, x) => {
                    acc.Add(new NSString(x.Key), new NSString(x.Value.LastOrDefault()));
                    return(acc);
                }),
                HttpMethod = request.Method.ToString().ToUpperInvariant(),
                Url        = NSUrl.FromString(request.RequestUri.AbsoluteUri),
            };

            var op = session.CreateDataTask(rq);

            cancellationToken.ThrowIfCancellationRequested();

            var ret = new TaskCompletionSource <HttpResponseMessage>();

            cancellationToken.Register(() => ret.TrySetCanceled());

            lock (inflightRequests) {
                inflightRequests[op] = new InflightOperation()
                {
                    FutureResponse    = ret,
                    Request           = request,
                    ResponseBody      = new ByteArrayListStream(),
                    CancellationToken = cancellationToken,
                };
            }

            op.Resume();
            var returnData = await ret.Task;

            setCookieContainerCookes();
            return(returnData);
        }
        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var headers = request.Headers as IEnumerable<KeyValuePair<string, IEnumerable<string>>>;
            var ms = new MemoryStream();

            if (request.Content != null) {
                await request.Content.CopyToAsync(ms).ConfigureAwait(false);
                headers = headers.Union(request.Content.Headers).ToArray();
            }

            var rq = new NSMutableUrlRequest() {
                AllowsCellularAccess = true,
                Body = NSData.FromArray(ms.ToArray()),
                CachePolicy = NSUrlRequestCachePolicy.UseProtocolCachePolicy,
                Headers = headers.Aggregate(new NSMutableDictionary(), (acc, x) => {
                    acc.Add(new NSString(x.Key), new NSString(x.Value.LastOrDefault()));
                    return acc;
                }),
                HttpMethod = request.Method.ToString().ToUpperInvariant(),
                Url = NSUrl.FromString(request.RequestUri.AbsoluteUri),
            };

            var op = session.CreateDataTask(rq);

            cancellationToken.ThrowIfCancellationRequested();

            var ret = new TaskCompletionSource<HttpResponseMessage>();
            cancellationToken.Register(() => ret.TrySetCanceled());

            lock (inflightRequests) {
                inflightRequests[op] = new InflightOperation() {
                    FutureResponse = ret,
                    Request = request,
                    ResponseBody = new ByteArrayListStream(),
                    CancellationToken = cancellationToken,
                };
            }

            op.Resume();
            return await ret.Task.ConfigureAwait(false);
        }
Beispiel #4
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var headers = request.Headers as IEnumerable <KeyValuePair <string, IEnumerable <string> > >;
            var ms      = new MemoryStream();

            if (request.Content != null)
            {
                await request.Content.CopyToAsync(ms).ConfigureAwait(false);

                headers = headers.Union(request.Content.Headers).ToArray();
            }

            // Add Cookie Header if any cookie for the domain in the cookie store
            var stringBuilder = new StringBuilder();

            var cookies = NSHttpCookieStorage.SharedStorage.Cookies
                          .Where(c => c.Domain == request.RequestUri.Host)
                          .ToList();

            foreach (var cookie in cookies)
            {
                stringBuilder.Append(cookie.Name + "=" + cookie.Value + ";");
            }

            var rq = new NSMutableUrlRequest()
            {
                AllowsCellularAccess = true,
                Body        = NSData.FromArray(ms.ToArray()),
                CachePolicy = (!this.DisableCaching ? NSUrlRequestCachePolicy.UseProtocolCachePolicy : NSUrlRequestCachePolicy.ReloadIgnoringCacheData),
                Headers     = headers.Aggregate(new NSMutableDictionary(), (acc, x) => {
                    if (x.Key == "Cookie")
                    {
                        foreach (var val in x.Value)
                        {
                            stringBuilder.Append(val + ";");
                        }
                    }
                    else
                    {
                        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),
            };

            if (stringBuilder.Length > 0)
            {
                var copy = new NSMutableDictionary(rq.Headers);
                copy.Add(new NSString("Cookie"), new NSString(stringBuilder.ToString().TrimEnd(';')));
                rq.Headers = copy;
            }

            if (Timeout != null)
            {
                rq.TimeoutInterval = Timeout.Value.Seconds;
            }

            var op = session.CreateDataTask(rq);

            cancellationToken.ThrowIfCancellationRequested();

            var ret = new TaskCompletionSource <HttpResponseMessage>();

            cancellationToken.Register(() => ret.TrySetCanceled());

            lock (inflightRequests)
            {
                inflightRequests[op] = new InflightOperation()
                {
                    FutureResponse    = ret,
                    Request           = request,
                    Progress          = getAndRemoveCallbackFromRegister(request),
                    ResponseBody      = new ByteArrayListStream(),
                    CancellationToken = cancellationToken,
                };
            }

            op.Resume();
            return(await ret.Task.ConfigureAwait(false));
        }