简体   繁体   English

使用 CefSharp C# 将 POST 数据发送到 URL

[英]Send POST data to URL with CefSharp C#

Im trying to work out how to send post data directly to a url with cefsharp.我正在尝试研究如何使用 cefsharp 将发布数据直接发送到 url。 Here is an example of what I want to send:这是我要发送的示例:

var values = new Dictionary<string, string>
{
     { "thing1", "hello" },
     { "thing2", "world" }
};
FormUrlEncodedContent content = new FormUrlEncodedContent(values);

Which will create thing1=hello&thing2=world I want to send this POST data to the url http://example.com/mydata.php using the existing browser with cefsharp.这将创建thing1=hello&thing2=world我想使用带有cefsharp的现有浏览器将此POST数据发送到url http://example.com/mydata.php

From what I can see从我所见

browser.Load("http://example.com/mydata.php");

Has no way to attach POST data, is there a way I can do this?无法附加POST数据,有没有办法做到这一点?

Basically I need to keep the same cookies that the browser already has, so if there is another way to do this for example using HttpWebRequest with the cefsharp ChromiumWebBrowser cookies then syncing them again after that request, that would also work, but i'm not sure if that is possible.基本上我需要保留浏览器已经拥有的相同 cookie,所以如果有另一种方法可以做到这一点,例如使用 HttpWebRequest 和 cefsharp ChromiumWebBrowser cookie 然后在该请求后再次同步它们,那也可以,但我不是确定这是否可能。

You can issue a POST using CefSharp via the LoadRequest method of the IFrame interface.您可以通过 IFrame 接口的 LoadRequest 方法使用 CefSharp 发出 POST。

For example, you can make an extension method that implements the equivalent of Microsoft's System.Windows.Forms.WebBrowser.Navigate(..., byte[] postData, string additionalHeaders) with something like例如,您可以创建一个扩展方法来实现 Microsoft 的System.Windows.Forms.WebBrowser.Navigate(..., byte[] postData, string additionalHeaders)例如

public 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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM