I want to make own browser application with own value of User-Agent.
Browser based on CHtmlView. MFC.
But exist strange problem with User-Agent from Ajax requests...
I did:
User-Agent value is used as argument to Navigate(). A Navigate() requests use right User-Agent.
Overload of OnAmbientProperty() method of CHtmlView class.
BOOL MyHtmlView::OnAmbientProperty(COleControlSite *pSite,
DISPID dispid, VARIANT *pvar)
{
USES_CONVERSION;
// Change user agent for this web browser host during hyperlinks
if (dispid == DISPID_AMBIENT_USERAGENT)
{
pvar->vt = VT_BSTR;
pvar->bstrVal = ::SysAllocString(m_userAgent);
return TRUE;
}
return CHtmlView::OnAmbientProperty(pSite, dispid, pvar);
}
This solve problem with hyper link.
But I have to use this browser for some Ajax application.
And here is problem. For Ajax requests it use original IE User-Agent value.
My PC is WinXP based with IE7.
Any idea how to solve this?
How to change User-Agent for any request from my browser?
Thanks!
On ajax request, you can set the HTTP header "User-Agent" : http://www.w3.org/TR/2007/WD-XMLHttpRequest-20070618/#dfn-setrequestheader
I solved problem with UrlMkSetSessionOption(), it changed IE settings for current session only:
const char ua[] = "My user agent string";
HRESULT rez = UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, (LPVOID)ua, sizeof(ua), 0);
Just one but, it change settings for once. Another calls return no error and no changes.
Some references:
Changing the user agent of the WebBrowser control
Related
I wrote a WP7 web browser app called "Cute Browser".
There is a problem that when navigating to an unkown content-type page such as 'application/stream', the WebBrowser is always navigating.
After my many experiments, I found out a method to detect the always navigating status using invoke the script:
var readyStateChange = function(){
if(document.readyState == 'interactive') {
document.detachEvent('onreadystatechange', readyStateChange);
window.external.Notify('MaybeWml');
}
}
if(document.readyState != 'complete'){
document.attachEvent('onreadystatechange', readyStateChange);
}
I want to know is any possible to get the Http Headers using WebBrowser without start another HttpRequest?
I have a local build of my site running at local.mydomain.com. I'm making ajax requests to api.mydomain.com which is running on an AWS server and returns JSON. In Chrome, I can call the API no problem. But in IE, I get Access Denied.
After researching, it seems to be a cross-(sub)domain restriction. But I was under the impression that this restriction would apply to both browsers. Can anybody see what might be going wrong here and why it might work in some browsers and not others?
It looks like the problem was in the transport object that IE8+ wants you to use. jQuery uses either ActiveXObject (for IE) or XMLHttpRequest (all others), but IE 8 and above requires XDomainRequest for ajax.
What you can do is return a custom xhr object via $.ajaxSettings.xhr like this,
// override xhr for browser that use XDR
if ('XDomainRequest' in window && window.XDomainRequest !== null) {
// override default jQuery transport
jQuery.ajaxSettings.xhr = function() {
try { return new XDomainRequest(); }
catch(e) {
console.log('test');
}
};
// also, override the support check
jQuery.support.cors = true;
}
I pulled this code from a discussion on the subject here:
http://graphicmaniacs.com/note/getting-a-cross-domain-json-with-jquery-in-internet-explorer-8-and-later/
Definitely take a look at that if you think you're experiencing the same problem.
We're developing a cross-platform application using PhoneGap. At one point, it makes an AJAX request to a relative path, which works fine on both Android and iOS, but not Windows Phone - it gets a 404 response. An absolute URL works fine. The following code:
var a = document.createElement('a');
a.setAttribute('href', 'personalData.html');
console.log(a.href);
also resolves to the correct absolute URL. However, the following:
var xhr = new XMLHttpRequest();
xhr.open("GET", "personalData.html", true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
console.log(xhr);
}
};
returns 404. I want to find out what's going on. Is there any way to know what absolute path XHR is requesting?
XMLHttpRequest is a JavaScript object that was designed by Microsoft and adopted by Mozilla, Apple, and Google, it's not related to Phonegap.
https://developer.mozilla.org/en/DOM/XMLHttpRequest
Said this, you could try using an http Proxy like Fiddler to view all http trafic.
http://www.fiddler2.com/fiddler2/
Best regards.
In these cases, Fiddler Web Debugger is unbeatable. It will tell you exactly what the request is doing.
It also works with the Windows Phone emulator. To debug an actual device, setup FIddler to accept external connections and assign Fiddler as a proxy on the phone.
I have done both scenarios, works fine.
Give it a shot.
I have try your code in my project (Phonegap/WinPhone7) and your code didn't get anything till I initialized the request (xhr.send();).
I have no idea how you make request without this method.
HI, is possible to add request headers in WP7 WebBrowser control?
There is no way to do this. If you need to change headers you'll need to use HttpWebRequest.
You could intercept the requests from the WebBrowser control and make them yourself via HWR but this could get complicated very quickly.
No - I don't think there's any API hook available for this.
It's a similar problem to the "change the user agent" request discussed in Bring back mobile version of website in WebBrowser control for wp7?
Sorry to necro but the answers here are wrong. Headers can be added to a WebBrowser through the Navigate method.
WebBrowser.Navigate(YourURI, null, YourCustomHeaderString)
See this page: http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff626636(v=vs.105).aspx
.
These headers will only apply to the first page navigated to through your code. If you want the headers to stay the same even when users click a link inside the web browser control, add this for the WebBrowser's navigating event:
private void browser_Navigating(object sender, NavigatingEventArgs e)
{
string url = e.Uri.ToString();
if(!url.Contains("YESHEADERS"))
{
e.Cancel = true;
string newUrl;
if(url.Contains("?"))
{
newUrl = url + "&YESHEADERS";
}
else
{
newUrl = url + "?YESHEADERS";
}
browser.Navigate(newUrl, null, "fore:" + Variables.GetForeground() + "")
}
}
Here's what that does:
We create an indicator, YESHEADERS, that tells us whether or not we have added custom headers.
When the WebBrowser tries to Navigate, we check whether or not the URL it is navigating to, e.Uri, contains YESHEADERS.
If it does, we've already added our headers. Take no action
If it does not, cancel the current navigation. Create a new URL equal to the old URL plus our indicator. We add YESHEADERS on to the new URL in it's query string. If you are not familiar with query strings that is fine, just know that they are extra strings on the URL that have no effect in our case. About Query Strings
Then, we navigate to the new URL, and add our custom headers.
In short, if we have our indicator YESHEADERS the web browser knows that we've added our custom headers, if we don't have YESHEADERS, than the web browser needs to add the headers.
How can I set HTTP headers in my Firefox extension?
I'll make it so these are only sent while hitting my site so I can detect if the plugin is installed or not and not promote the plugin if it is.
Here's the most compact way I found to make this work:
Components.classes["#mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService ).addObserver({
observe : function(subject, topic, data) {
var channel = subject.QueryInterface( Components.interfaces.nsIHttpChannel );
if ( /mysite/.test( channel.originalURI.host ) ) {
channel.setRequestHeader("x-mysite-extended", "true", false);
}
}
},"http-on-modify-request",false);
There are a few existing Firefox extensions that modify HTTP headers en route to the server, and at least one of them, modifyheaders, has open source code.
Or, of course, there's the relevant page in the Mozilla Developer Center, Setting HTTP request headers.