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;
                    }
                });
        }
Esempio n. 2
0
            public string FetchDocument(int page)
            {
                var p = page > 1 ? "&p=" + page : "";
                var uri = string.Format("http://www.dzone.com/links/index.html?type=html{0}", p);

                /* WebClient doesn't work because libc.dylib is not being linked properly by MonoMac
                 * Use Cocoa NSUrlRequest instead as a not-so-pretty-fix
                 *
                var web = new WebClient();
                web.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 5.1; U; rv:5.0) Gecko/20100101 Firefox/5.0";

                return web.DownloadString(uri);
                */

                var pool = new NSAutoreleasePool();

                NSError error = null;
                NSUrlResponse response = null;
                NSMutableUrlRequest request = new NSMutableUrlRequest(NSUrl.FromString(uri));
                var headerKeys = new NSObject[] {
                    (NSString)"Accept",
                    (NSString)"Accept-Charset",
                    (NSString)"Accept-Encoding",
                    (NSString)"Accept-Language",
                    (NSString)"Cache-Control",
                    (NSString)"Host",
                    (NSString)"User-Agent"
                };
                var headerVals = new NSObject[] {
                    (NSString)"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                    (NSString)"ISO-8859-1,utf-8;q=0.7,*;q=0.3",
                    (NSString)"deflate",
                    (NSString)"en-US,en;q=0.8",
                    (NSString)"max-age=0",
                    (NSString)"www.dzone.com",
                    (NSString)"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.21 Safari/535.7"
                };
                request.Headers = NSDictionary.FromObjectsAndKeys(headerVals, headerKeys);
                var data = NSUrlConnection.SendSynchronousRequest(request, out response, out error);
                var html = (string)NSString.FromData(data, NSStringEncoding.UTF8);

                pool.Dispose();

                return html;
            }