I am new to "Web Programming" and I would like to know if it is possible to handle POST request and response manually in CEFSharp. The Scenario is that the website that I am interacting with using CEFSharp is creating the request, But I want to take control after that if it is possible. I Imagine that ResourceRequestHandler must be some thing like the follwing, which one must convert the CEFSharp IRequest to .NET HttpRequestMessage somehow:
public class CustomResourceRequestHandler : CefSharp.Handler.ResourceRequestHandler
{
private static readonly HttpClient hc = new();
private readonly AutoResetEvent are;
public CustomResourceRequestHandler(AutoResetEvent are)
{
this.are = are;
}
protected override CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
var hrm = new HttpRequestMessage(HttpMethod.Post, request.Url);
//How to add Headers properly?
foreach (string rh in request.Headers)
hrm.Headers.Add(rh, request.Headers[rh]);
//How to add content properly?
Task.Factory.StartNew(async() =>
{
are.WaitOne();
var response = hc.SendAsync(hrm);
await response;
// Do Something with response.Result
});
return CefReturnValue.Cancel;
}
}
The questions are: First, Is this even possible? since the underlying socket is changed, doesn't the webserver complain? Second, How to copy IRequest to HttpRequestMessage Properly? I Added the headers, and don't know if it's done correctly, But I am struggling to convert the content field. Third, Is copying Headers and Content enough?