/// <summary>
        /// Downloads the specified <paramref name="url"/> and calls <paramref name="completeHandler"/>
        /// when the download is complete. Makes a GET Request.
        /// </summary>
        /// <param name="frame">valid frame</param>
        /// <param name="url">url to download</param>
        /// <param name="completeHandler">Action to be executed when the download is complete.</param>
        public static void DownloadUrl(this IFrame frame, string url, Action <IUrlRequest, Stream> completeHandler)
        {
            if (!frame.IsValid)
            {
                throw new Exception("Frame is invalid, unable to continue.");
            }

            //Can be created on any valid CEF Thread, here we'll use the CEF UI Thread
            Cef.UIThreadTaskFactory.StartNew(delegate
            {
                var request = frame.CreateRequest(false);

                request.Method = "GET";
                request.Url    = url;

                var memoryStream = new MemoryStream();

                var urlRequestClient = Fluent.UrlRequestClient
                                       .Create()
                                       .OnDownloadData((req, stream) =>
                {
                    stream.CopyTo(memoryStream);
                })
                                       .OnRequestComplete((req) =>
                {
                    memoryStream.Position = 0;

                    completeHandler?.Invoke(req, memoryStream);
                })
                                       .Build();

                var urlRequest = frame.CreateUrlRequest(request, urlRequestClient);
            });
        }
示例#2
0
        void f_post( )
        {
            string data   = File.ReadAllText("post-data.txt");
            string header = File.ReadAllText("post-header.txt");

            string url = "https://azure.microsoft.com/en-us/cognitive-services/demo/websearchapi/";

            IFrame   frame   = browser.GetMainFrame();
            IRequest request = frame.CreateRequest();

            request.Url    = url;
            request.Method = "POST";

            request.InitializePostData();
            var element = request.PostData.CreatePostDataElement();

            //element.Bytes = req.PostData.;
            request.PostData.AddElement(element);

            NameValueCollection headers = new NameValueCollection();

            //headers.Add("Content-Type", contentType);
            request.Headers = headers;

            frame.LoadRequest(request);
        }
示例#3
0
        public bool OnBeforePopup(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
        {
            var chromiumWebBrowser = (ExtChromiumBrowser)browserControl;

            chromiumWebBrowser.Dispatcher.Invoke(new Action(() =>
            {
                BrowserPopupWin win = new BrowserPopupWin();
                win.ShowInTaskbar   = false;
                win.Height          = 0;
                win.Width           = 0;
                win.Show();

                IntPtr handle = new WindowInteropHelper(win).Handle;
                windowInfo.SetAsChild(handle);

                _scheduler.Run(() =>
                {
                    WaitUtil.Wait(() => chromiumWebBrowser.PostData);

                    IRequest request = null;
                    if (chromiumWebBrowser.PostData != null)
                    {
                        request        = frame.CreateRequest();
                        request.Url    = targetUrl;
                        request.Method = "POST";

                        request.InitializePostData();
                        var element   = request.PostData.CreatePostDataElement();
                        element.Bytes = chromiumWebBrowser.PostData;
                        request.PostData.AddElement(element);
                        chromiumWebBrowser.PostData = null;
                    }

                    chromiumWebBrowser.Dispatcher.Invoke(new Action(() =>
                    {
                        NewWindowEventArgs e = new NewWindowEventArgs(targetUrl, request);
                        chromiumWebBrowser.OnNewWindow(e);
                    }));

                    chromiumWebBrowser.Dispatcher.Invoke(new Action(() =>
                    {
                        win.Close();
                    }));
                });
            }));

            newBrowser = null;
            return(false);
        }
        public static void Navigate(this IWebBrowser browser, byte[] postDataBytes, IRequest requestCopy)
        {
            IFrame   frame   = browser.GetBrowser().MainFrame;
            IRequest request = frame.CreateRequest();

            request.Url    = requestCopy.Url;
            request.Method = "POST";

            request.InitializePostData();
            var element = request.PostData.CreatePostDataElement();

            element.Bytes = postDataBytes;
            request.PostData.AddElement(element);

            request.Headers = requestCopy.Headers;

            frame.LoadRequest(request);
        }
        /// <summary>
        /// Downloads the specified <paramref name="url"/> as a <see cref="byte[]"/>.
        /// Makes a GET Request.
        /// </summary>
        /// <param name="frame">valid frame</param>
        /// <param name="url">url to download</param>
        /// <returns>A task that can be awaited to get the <see cref="byte[]"/> representing the Url</returns>
        public static Task <byte[]> DownloadUrlAsync(this IFrame frame, string url)
        {
            if (!frame.IsValid)
            {
                throw new Exception("Frame is invalid, unable to continue.");
            }

            var taskCompletionSource = new TaskCompletionSource <byte[]>();

            //Can be created on any valid CEF Thread, here we'll use the CEF UI Thread
            Cef.UIThreadTaskFactory.StartNew(delegate
            {
                var request = frame.CreateRequest(false);

                request.Method = "GET";
                request.Url    = url;

                var memoryStream = new MemoryStream();

                var urlRequestClient = Fluent.UrlRequestClient
                                       .Create()
                                       .OnDownloadData((req, stream) =>
                {
                    stream.CopyTo(memoryStream);
                })
                                       .OnRequestComplete((req) =>
                {
                    if (req.RequestStatus == UrlRequestStatus.Success)
                    {
                        taskCompletionSource.TrySetResultAsync(memoryStream.ToArray());
                    }
                    else
                    {
                        taskCompletionSource.TrySetExceptionAsync(new Exception("RequestStatus:" + req.RequestStatus + ";StatusCode:" + req.Response.StatusCode));
                    }
                })
                                       .Build();

                var urlRequest = frame.CreateUrlRequest(request, urlRequestClient);
            });

            return(taskCompletionSource.Task);
        }
示例#6
0
        public static void Navigate(this IWebBrowser browser, string url, byte[] postDataBytes, string contentType)
        {
            IFrame   frame   = browser.GetMainFrame();
            IRequest request = frame.CreateRequest();

            request.Url    = url;
            request.Method = "POST";

            request.InitializePostData();
            var element = request.PostData.CreatePostDataElement();

            element.Bytes = postDataBytes;
            request.PostData.AddElement(element);

            NameValueCollection headers = new NameValueCollection();

            headers.Add("Content-Type", contentType);
            request.Headers = headers;

            frame.LoadRequest(request);
        }