我试图找出如何使用cefsharp将帖子数据直接发送到网址.以下是我要发送的示例:
var values = new Dictionary<string, string>
{
{ "thing1", "hello" },
{ "thing2", "world" }
};
FormUrlEncodedContent content = new FormUrlEncodedContent(values);
Run Code Online (Sandbox Code Playgroud)
这将创建thing1=hello&thing2=world
我想http://example.com/mydata.php使用现有的cefsharp浏览器将此POST数据发送到URL .
从我所看到的
browser.Load("http://example.com/mydata.php");
Run Code Online (Sandbox Code Playgroud)
无法附加POST数据,有没有办法可以做到这一点?
基本上我需要保留浏览器已经拥有的相同的cookie,所以如果有另一种方法可以做到这一点,例如使用HttpWebRequest和cefsharp ChromiumWebBrowser cookie然后在该请求之后再次同步它们,那也可以,但我不是确定是否可行.
您可以通过IFrame接口的LoadRequest方法使用CefSharp发出POST.
例如,您可以创建一个扩展方法,实现与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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5791 次 |
| 最近记录: |