TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
manektech development
NA
127
6.8k
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
public
class
CustomResourceRequestHandler : ResourceRequestHandler
{
private
string
_signature;
public
CustomResourceRequestHandler(
string
signature)
{
_signature = GetSetComanDetails.Signature;
}
protected
override
CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
if
(request.Url.Contains(
"https://****.com"
))
{
if
(!
string
.IsNullOrEmpty(GetSetComanDetails.Signature))
{
var headers = request.Headers;
headers[
"s"
] = GetSetComanDetails.Signature;
request.Headers = headers;
return
CefReturnValue.Continue;
}
else
return
base
.OnBeforeResourceLoad(chromiumWebBrowser, browser, frame, request, callback);
}
else
return
base
.OnBeforeResourceLoad(chromiumWebBrowser, browser, frame, request, callback);
}
}
public
class
CustomRequestHandler : RequestHandler
{
private
string
_signature;
public
CustomRequestHandler(
string
signature)
{
_signature = GetSetComanDetails.Signature;
}
protected
override
IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request,
bool
isNavigation,
bool
isDownload,
string
requestInitiator,
ref
bool
disableDefaultHandling)
{
if
(request.Url.Contains(
"https://****.com"
) && request.Url.Contains(
".php"
))
{
if
(!
string
.IsNullOrEmpty(GetSetComanDetails.Signature))
return
new
CustomResourceRequestHandler(GetSetComanDetails.Signature);
else
return
base
.GetResourceRequestHandler(chromiumWebBrowser, browser, frame, request, isNavigation, isDownload, requestInitiator,
ref
disableDefaultHandling);
}
else
return
base
.GetResourceRequestHandler(chromiumWebBrowser, browser, frame, request, isNavigation, isDownload, requestInitiator,
ref
disableDefaultHandling);
}
}
This event is called while we call the URL on the CefSharp browser in WPF..
chromium = (ChromiumWebBrowser)
this
.FindName(
"cefSharpWebBrowser"
);
cefSharpWebBrowser.Address =
"https://****/access.php?type=desktop"
;
chromium.RequestHandler =
new
CustomRequestHandler(
""
);
Try 2
Implement the below interface
CefSharp/IRequestHandler.cs at cefsharp/43 · cefsharp/CefSharp · GitHub[^]
Try 3
chromium.ResourceHandlerFactory =
new
MyResourceHandlerFactory();
...
class
MyResourceHandlerFactory :IResourceHandlerFactory
{
public
bool
HasHandlers
{
get
{
return
true
; }
}
public
IResourceHandler GetResourceHandler(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request)
{
if
(request.Url.StartsWith(
"http"
))
return
new
MyResourceHandler();
return
null
;
}
}
...
class
MyResourceHandler : IResourceHandler
{
private
HttpWebRequest webRequest;
private
HttpWebResponse webResponse;
private
Stream requestStream;
private
byte
[] requestBytes;
ICallback callback;
public
Stream GetResponse(IResponse response,
out
long
responseLength,
out
string
redirectUrl)
{
responseLength = webResponse.ContentLength;
redirectUrl =
null
;
string
type = webResponse.ContentType;
// strip off the encoding, if present
if
(type.IndexOf(
"; "
)>0)
type = type.Substring(0,type.IndexOf(
"; "
));
response.MimeType = type;
// only a direct assignment works here, don't try to use other methods of the name/value collection;
response.ResponseHeaders = webResponse.Headers;
response.StatusCode = (
int
)webResponse.StatusCode;
response.StatusText = webResponse.StatusDescription;
// TODO return a wrapper around this stream to capture the response inline.
return
webResponse.GetResponseStream();
}
private
void
SendRequestBody(IAsyncResult result)
{
requestStream = webRequest.EndGetRequestStream(result);
requestStream.BeginWrite(requestBytes, 0, requestBytes.Length, RequestSent,
null
);
}
private
void
RequestSent(IAsyncResult ar)
{
requestStream.EndWrite(ar);
requestStream.Close();
webRequest.BeginGetResponse(
new
AsyncCallback(Response),
null
);
}
private
void
Response(IAsyncResult ar)
{
webResponse = (HttpWebResponse)webRequest.EndGetResponse(ar);
callback.Continue();
}
public
bool
ProcessRequestAsync(IRequest request, ICallback callback)
{
this
.callback = callback;
webRequest = (HttpWebRequest)WebRequest.Create(request.Url);
webRequest.Method = request.Method;
foreach
(
string
key
in
request.Headers)
{
string
value = request.Headers[key];
// HttpWebRequest doesn't like it if you try to set these values through the Headers collection
if
(
"Accept"
.Equals(key))
webRequest.Accept = value;
else
if
(
"User-Agent"
.Equals(key))
webRequest.UserAgent = value;
else
if
(
"Referer"
.Equals(key))
webRequest.Referer = value;
else
if
(
"Content-Type"
.Equals(key))
webRequest.ContentType = value;
else
if
(
"Content-Length"
.Equals(key))
webRequest.ContentLength=Convert.ToInt32(value);
else
webRequest.Headers.Add(key, value);
}
if
(!String.IsNullOrWhiteSpace(request.Body))
{
this
.requestBytes = Encoding.UTF8.GetBytes(request.Body);
webRequest.BeginGetRequestStream(
new
AsyncCallback(SendRequestBody),
null
);
}
else
{
webRequest.BeginGetResponse(
new
AsyncCallback(Response),
null
);
}
return
true
;
}
Reply
Answers (
0
)
AES Decryption in Java and C Sharp
How to create streaming Web api?