How to pass the header from cefsharpwebbrowser .

May 2 2020 12:29 PM
I m trying to pass the header value in the PHP files when the browser(cefSharpWebBrowser) request for the web page in WPF C#.
 
In my PHP code file, I write below logic.
 
able to view HTTP headers accessing https://www.****.com/temp_header_check.php, which can be accessed from this path:
 
this line means to display all the headers which we passed to this PHP files
 
What I have tried:
 
I was tried below code for the pass the header values to each requested PHP files at BeforeResourceLoad event of browser. On this code, I didn't get any error but I can't see the I passed headers values to the PHP files.
 
Try 1
  1. public class CustomResourceRequestHandler : ResourceRequestHandler  
  2. {  
  3. private string _signature;  
  4. public CustomResourceRequestHandler(string signature)  
  5. {  
  6. _signature = GetSetComanDetails.Signature;  
  7. }  
  8.   
  9. protected override CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)  
  10. {  
  11. if (request.Url.Contains("https://****.com"))  
  12. {  
  13. if (!string.IsNullOrEmpty(GetSetComanDetails.Signature))  
  14. {  
  15. var headers = request.Headers;  
  16. headers["s"] = GetSetComanDetails.Signature;  
  17. request.Headers = headers;  
  18. return CefReturnValue.Continue;  
  19. }  
  20. else return base.OnBeforeResourceLoad(chromiumWebBrowser, browser, frame, request, callback);  
  21. }  
  22. else return base.OnBeforeResourceLoad(chromiumWebBrowser, browser, frame, request, callback);  
  23. }  
  24.   
  25. }  
  26. public class CustomRequestHandler : RequestHandler  
  27. {  
  28. private string _signature;  
  29. public CustomRequestHandler(string signature)  
  30. {  
  31. _signature = GetSetComanDetails.Signature;  
  32. }  
  33.   
  34. protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)  
  35. {  
  36. if (request.Url.Contains("https://****.com") && request.Url.Contains(".php"))  
  37. {  
  38. if (!string.IsNullOrEmpty(GetSetComanDetails.Signature)) return new CustomResourceRequestHandler(GetSetComanDetails.Signature);  
  39. else return base.GetResourceRequestHandler(chromiumWebBrowser, browser, frame, request, isNavigation, isDownload, requestInitiator, ref disableDefaultHandling);  
  40. }  
  41. else return base.GetResourceRequestHandler(chromiumWebBrowser, browser, frame, request, isNavigation, isDownload, requestInitiator, ref disableDefaultHandling);  
  42. }  
  43. }  
This event is called while we call the URL on the CefSharp browser in WPF..
  1. chromium = (ChromiumWebBrowser)this.FindName("cefSharpWebBrowser");  
  2. cefSharpWebBrowser.Address = "https://****/access.php?type=desktop";  
  3. chromium.RequestHandler = new CustomRequestHandler("");  
Try 2
 
Implement the below interface
 
CefSharp/IRequestHandler.cs at cefsharp/43 · cefsharp/CefSharp · GitHub[^]
 
Try 3
  1. chromium.ResourceHandlerFactory = new MyResourceHandlerFactory();  
  2. ...  
  3. class MyResourceHandlerFactory :IResourceHandlerFactory  
  4. {  
  5. public bool HasHandlers  
  6. {  
  7. get { return true; }  
  8. }  
  9. public IResourceHandler GetResourceHandler(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request)  
  10. {  
  11. if (request.Url.StartsWith("http"))  
  12. return new MyResourceHandler();  
  13. return null;  
  14. }  
  15. }  
  16.   
  17. ...  
  18.   
  19. class MyResourceHandler : IResourceHandler  
  20. {  
  21. private HttpWebRequest webRequest;  
  22. private HttpWebResponse webResponse;  
  23. private Stream requestStream;  
  24. private byte[] requestBytes;  
  25. ICallback callback;  
  26.   
  27. public Stream GetResponse(IResponse response, out long responseLength, out string redirectUrl)  
  28. {  
  29. responseLength = webResponse.ContentLength;  
  30. redirectUrl = null;  
  31. string type = webResponse.ContentType;  
  32. // strip off the encoding, if present  
  33. if (type.IndexOf("; ")>0)  
  34. type = type.Substring(0,type.IndexOf("; "));  
  35. response.MimeType = type;  
  36. // only a direct assignment works here, don't try to use other methods of the name/value collection;  
  37. response.ResponseHeaders = webResponse.Headers;  
  38. response.StatusCode = (int)webResponse.StatusCode;  
  39. response.StatusText = webResponse.StatusDescription;  
  40. // TODO return a wrapper around this stream to capture the response inline.  
  41. return webResponse.GetResponseStream();  
  42. }  
  43.   
  44. private void SendRequestBody(IAsyncResult result)  
  45. {  
  46. requestStream = webRequest.EndGetRequestStream(result);  
  47. requestStream.BeginWrite(requestBytes, 0, requestBytes.Length, RequestSent, null);  
  48. }  
  49.   
  50. private void RequestSent(IAsyncResult ar)  
  51. {  
  52. requestStream.EndWrite(ar);  
  53. requestStream.Close();  
  54. webRequest.BeginGetResponse(new AsyncCallback(Response), null);  
  55. }  
  56.   
  57. private void Response(IAsyncResult ar)  
  58. {  
  59. webResponse = (HttpWebResponse)webRequest.EndGetResponse(ar);  
  60. callback.Continue();  
  61. }  
  62.   
  63. public bool ProcessRequestAsync(IRequest request, ICallback callback)  
  64. {  
  65. this.callback = callback;  
  66. webRequest = (HttpWebRequest)WebRequest.Create(request.Url);  
  67. webRequest.Method = request.Method;  
  68. foreach (string key in request.Headers)  
  69. {  
  70. string value = request.Headers[key];  
  71. // HttpWebRequest doesn't like it if you try to set these values through the Headers collection  
  72. if ("Accept".Equals(key))  
  73. webRequest.Accept = value;  
  74. else if ("User-Agent".Equals(key))  
  75. webRequest.UserAgent = value;  
  76. else if ("Referer".Equals(key))  
  77. webRequest.Referer = value;  
  78. else if ("Content-Type".Equals(key))  
  79. webRequest.ContentType = value;  
  80. else if ("Content-Length".Equals(key))  
  81. webRequest.ContentLength=Convert.ToInt32(value);  
  82. else  
  83. webRequest.Headers.Add(key, value);  
  84. }  
  85. if (!String.IsNullOrWhiteSpace(request.Body))  
  86. {  
  87. this.requestBytes = Encoding.UTF8.GetBytes(request.Body);  
  88. webRequest.BeginGetRequestStream(new AsyncCallback(SendRequestBody), null);  
  89. }  
  90. else  
  91. {  
  92. webRequest.BeginGetResponse(new AsyncCallback(Response), null);  
  93. }  
  94. return true;  
  95. }