I am using a web interface within my c# windows form application using Cefsharp libraries.I can load my tab page with the browser using the following code
Cef.EnableHighDPISupport();
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
chromeBrowser = new ChromiumWebBrowser("http://localhost/myproject/login.php");
tab_web.Controls.Add(chromeBrowser);
But I am unable to post the login credential to the page (metod is POST) so that i can register the session and the user can direct access to his profile from the application. UPDATE
public partial class Form1 : Form
{
public void InitializeChromium()
{
Cef.EnableHighDPISupport();
CefSettings settings = new CefSettings();
settings.RemoteDebuggingPort = 8088;
Cef.Initialize(settings);
ChromiumWebBrowser chromeBrowser = new ChromiumWebBrowser("http://localhost/test.php");
chromeBrowser.RequestHandler = new CustomRequestHandler();
tab_web.Controls.Add(chromeBrowser);
chromeBrowser.Dock = DockStyle.Fill;
}
public Form1()
{
InitializeComponent();
InitializeChromium();
}
public void load_browser()
{
ChromiumWebBrowser chromeBrowser = new ChromiumWebBrowser();
byte[] request = Encoding.ASCII.GetBytes("data1=sssss&data2=sssss");
PostTest.Navigate( chromeBrowser, "http://localhost/test.php", request, "application/x-www-form-urlencoded");
tab_gis.Controls.Add(chromeBrowser);
}
}
public class CustomRequestHandler : CefSharp.Handler.RequestHandler
{
protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
{
if (request.Url == "http://localhost/test.php")
{
return new CustomResourceRequestHandler();
}
return null;
}
}
public class CustomResourceRequestHandler : CefSharp.Handler.ResourceRequestHandler
{
protected override CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
var postData = new PostData();
MessageBox.Show("HHHH");
postData.AddData("test=123&data2=456");
request.Method = "POST";
request.PostData = postData;
return CefReturnValue.Continue;
}
}
screenshot of the reply from the httpbin is as attached
Meanwhile the form data submission should be as follows
I changed the content type to multipart/form-data , text, xml, etc. nothing helped me.
Credit to Amaitland. He gives the necessary directions with examples through which I got the solution of this problem. I am posting the working code here for help to any other Cefsharp user.
public class CustomRequestHandler : CefSharp.Handler.RequestHandler
{
protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
{
//Where possible only intercept specific Url's
//Load http://httpbin.org/post in the browser and you'll
//see the post data
if (request.Url == "http://httpbin.org/post")
{
return new CustomResourceRequestHandler();
}
//Default behaviour, url will be loaded normally.
return null;
}
}
public class CustomResourceRequestHandler : CefSharp.Handler.ResourceRequestHandler
{
protected override CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
//Modify the request to add post data
//Make sure to read https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
var postData = new PostData();
postData.AddData("test=123&data=456");
request.Method = "POST";
request.PostData = postData;
//Set the Content-Type header to whatever suites your requirement
request.SetHeaderByName("Content-Type", "application/x-www-form-urlencoded", true);
//Set additional Request headers as required.
return CefReturnValue.Continue;
}
}
//Load http://httpbin.org/post in the browser to see the post data
browser = new ChromiumWebBrowser("http://httpbin.org/post");
browser.RequestHandler = new CustomRequestHandler();
For more details, pl refer https://github.com/cefsharp/CefSharp/wiki/General-Usage#load-url-with-postdata