使用CefSharp C#将POST数据发送到URL [英] Send POST data to URL with CefSharp C#

查看:197
本文介绍了使用CefSharp C#将POST数据发送到URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出如何使用cefsharp将帖子数据直接发送到url.这是我要发送的示例:

Im trying to work out how to send post data directly to a url with cefsharp. 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);

这将创建 thing1 = hello& thing2 = world 我想使用带有cefsharp的现有浏览器将此POST数据发送到url http://example.com/mydata.php .

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.

据我所见

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

无法附加POST数据,有办法吗?

Has no way to attach POST data, is there a way I can do this?

基本上,我需要保留浏览器已经具有的cookie,因此,如果有另一种方法,例如将HttpWebRequest与cefsharp ChromiumWebBrowser cookie一起使用,然后在该请求之后再次同步它们,那也可以,但是我我不确定这是否可能.

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.

推荐答案

您可以通过IFrame接口的LoadRequest方法使用CefSharp发出POST.

You can issue a POST using CefSharp via the LoadRequest method of the IFrame interface.

例如,您可以创建一个扩展方法,该扩展方法使用类似于以下内容的方式实现与Microsoft的 System.Windows.Forms.WebBrowser.Navigate(...,byte [] postData,字符串AdditionalHeaders)等效的方法

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

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

这篇关于使用CefSharp C#将POST数据发送到URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆