示例#1
0
        /// <summary>
        /// Send a POST request to the specified <see cref="Uri"/> with a cancellation token as an asynchronous operation.
        /// </summary>
        /// <param name="requestUri">The <see cref="Uri"/> the request is sent to.</param>
        /// <param name="referrerUri">
        /// The <see cref="Uri"/> of the referring site for a request. Can be null.
        /// </param>
        /// <param name="referrerPolicy">
        /// The policy for how the Referrer HTTP header value will be sent during request.
        /// </param>
        /// <param name="headers">
        /// A <see cref="NameValueCollection"/> containing header name/value pairs associated with a request. Can be null.
        /// </param>
        /// <param name="content">The request content sent to the specified <see cref="Uri"/>.</param>
        /// <param name="cancellationToken">
        /// A cancellation token that can be used by other objects or threads to receive notice of cancellation.
        /// </param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        public async Task <CefNetWebRequest> PostAsync(Uri requestUri, Uri referrerUri, CefReferrerPolicy referrerPolicy, CefPostData content, NameValueCollection headers, CancellationToken cancellationToken)
        {
            if (requestUri is null)
            {
                throw new ArgumentNullException(nameof(requestUri));
            }

            var r = new CefRequest();

            r.Flags = (int)this.RequestFlags;
            if (referrerUri != null)
            {
                r.SetReferrer(referrerUri.AbsoluteUri, referrerPolicy);
            }
            if (headers != null && headers.Count > 0)
            {
                using (var map = new CefStringMultimap())
                {
                    map.Add(headers);
                    r.Set(requestUri.AbsoluteUri, "POST", content, map);
                }
            }
            else
            {
                r.Url      = requestUri.AbsoluteUri;
                r.Method   = "POST";
                r.PostData = content;
            }

            var request = new CefNetWebRequest(this);
            await request.SendAsync(r, _context, cancellationToken);

            return(request);
        }
        protected override bool OnBeforePopup(CefBrowser browser, CefFrame frame, string targetUrl, string targetFrameName, CefPopupFeatures popupFeatures, CefWindowInfo windowInfo, ref CefClient client, CefBrowserSettings settings, ref bool noJavascriptAccess)
        {
            bool res = false;

            if (!string.IsNullOrEmpty(targetUrl))
            {
                if (webBrowser.selfRequest != null)
                {
                    CefRequest req = CefRequest.Create();
                    req.FirstPartyForCookies = webBrowser.selfRequest.FirstPartyForCookies;
                    req.Options = webBrowser.selfRequest.Options;

                    /*CefPostData postData = CefPostData.Create();
                     * CefPostDataElement element = CefPostDataElement.Create();
                     * int index = targetUrl.IndexOf("?");
                     * string url = targetUrl.Substring(0, index);
                     * string data = targetUrl.Substring(index + 1);
                     * byte[] bytes = Encoding.UTF8.GetBytes(data);
                     * element.SetToBytes(bytes);
                     * postData.Add(element);
                     */
                    System.Collections.Specialized.NameValueCollection h = new System.Collections.Specialized.NameValueCollection();
                    h.Add("Content-Type", "application/x-www-form-urlencoded");
                    req.Set(targetUrl, webBrowser.selfRequest.Method, null, webBrowser.selfRequest.GetHeaderMap());
                    webBrowser.selfRequest = req;
                }
                webBrowser.selfRequest.Set(targetUrl, webBrowser.selfRequest.Method, webBrowser.selfRequest.PostData, webBrowser.selfRequest.GetHeaderMap());
                if (webBrowser.isOpenNewWindow)
                {
                    res = webBrowser.OnNewWindow(targetUrl);
                }
                else
                {
                    webBrowser.OpenUrl(targetUrl);
                    res = true;
                }
                if (res)
                {
                    return(res);
                }
            }
            res = base.OnBeforePopup(browser, frame, targetUrl, targetFrameName, popupFeatures, windowInfo, ref client, settings, ref noJavascriptAccess);
            return(res);
        }