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.
Related
I have a webview inside my application and when an external link is clicked (that in normal browser is open in a new tab), I can't then go back to my website.
It is possible when a new tab is open to have the menu closed that tab like Gmail do ?
The objective is that, whenever a link is clicked, the user would have the choice to choose which option to view the content with, e.g. Clicking a link would suggest open youtube app or google chrome. The purpose is to appear the google chrome option
Or what suggestions do you have to handle this situation ?
If I understood you correctly, you want to have the option to select how to open the web link - inside your app, or within another app's (browser) context.
If this is correct, then you can use Xamarin.Essentials: Browser functionality.
public async Task OpenBrowser(Uri uri)
{
await Browser.OpenAsync(uri, BrowserLaunchMode.SystemPreferred);
}
Here the important property is the BrowserLaunchMode flag, which you can learn more about here
Basically, you have 2 options - External & SystemPreferred.
The first one is clear, I think - it will open the link in an external browser.
The second options takes advantage of Android's Chrome Custom Tabs & for iOS - SFSafariViewController
P.S. You can also customise the PreferredToolbarColor, TitleMode, etc.
Edit: Based from your feedback in the comments, you want to control how to open href links from your website.
If I understood correctly, you want the first time that you open your site, to not have the nav bar at the top, and after that to have it. Unfortunately, this is not possible.
You can have the opposite behaviour achieved - the first time that you open a website, to have the nav bar and if the user clicks on any link, to open it externally (inside a browser). You have 2 options for this:
To do it from your website - change the a tag's target to be _blank like this;
To do it from your mobile app - create a Custom renderer for the WebView. In the Android project's renderer implementation, change the Control's WebViewClient like so:
public class CustomWebViewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, IWebResourceRequest request)
{
Intent intent = new Intent(Intent.ActionView, request.Url);
CrossCurrentActivity.Current.StartActivity(intent);
return true;
}
}
In the app I'm working on, I want to be able to open the webbrowser and navigate to a specific URL.
This is easy enough using the WebBrowserTask, but the URL I want to reach requires a couple of request headers.
I haven't been able to figure out how to achieve this, and is of course hoping someone here can point me in the right direction, or even show an example.
If the task it self doesn't support headers, is there any other way?
EDIT:
Well I found out that the WebBrowserTask in its current state does not support adding headers.
My solution was to make a new XAML page in the app and add a WebBrowser control as the only object.
The webbrowser control supports adding headers like this:
Uri uri = new Uri("http://YOUR_URL.COM");
string headers = String.Format("HEADER1:{0}\r\nHEADER2:{1}\r\nHEADER3:{2}\r\n", projectId, username, password);
webBrowser.Navigate(uri, null, headers);
The important part of adding the headers is to remember to seperate the headers with \r\n
I'm trying to capture and handle every single request a web page, or a plugin in it is about to make.
For example, if you open the console, and enable Net logging, when a HTTP request is about to be sent, console shows it there.
I want to capture every link and call my function even when a video is loaded by flash player (which is logged in console also, if it is http).
Can anyone guide me what I should do, or where I should get started?
Edit: I want to be able to cancel the request and handle it my way if needed.
You can use the Jetpack SDK to get most of what you need, I believe. If you register to system events and listen for http-on-modify-request, you can use the nsIHttpChannel methods to modify the response and request
let { Ci } = require('chrome');
let { on } = require('sdk/system/events');
let { newURI } = require('sdk/url/utils');
on('http-on-modify-request', function ({subject, type, data}) {
if (/google/.test(subject.URI.spec)) {
subject.QueryInterface(Ci.nsIHttpChannel);
subject.redirectTo(newURI('http://mozilla.org'));
}
});
Additional info, "Intercepting Page Loads"
non sdk version and with much much more control and detail:
this allows you too look at the flags so you can only watch LOAD_DOCUMENT_URI which is frames and main window. main window is always LOAD_INITIAL_DOCUMENT_URI
https://github.com/Noitidart/demo-on-http-examine
https://github.com/Noitidart/demo-nsITraceableChannel - in this one you can see the source before it is parsed by the browser
in these examples you see how to get the contentWindow and browserWindow from the subject as well, you can apply this to sdk example, just use the "subject"
also i prefer to use http-on-examine-response, even in sdk version. because otherwise you will see all the pages it redirects FROM, not the final redirect TO. say a url blah.com redirects you to blah.com/1 and then blah.com/2
only blah.com/2 has a document, so on modify you see blah.com and blah.com/1, they will have flags LOAD_REPLACE, typically they redirect right away so the document never shows, if it is a timed redirect you will see the document and will also see LOAD_INITIAL_DOCUMENT_URI flag, im guessing i havent experienced it myself
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've developed a WP7 client that uses the Facebook C# SDK, using OAUTH and the web browser control.
Everything works fine except that on the page where the user is requested to accept/reject the access permissions I am asking for, the "Don't Allow" and "Allow" buttons are off the bottom of the browser's screen, and it isn't obvious that the user must scroll down to click on them.
I've tried using all the different display modes (touch, wap, page, popup) and "page" is the only one that shows the buttons on the same page, but then the fonts are tiny. I've also tried different sizes for the browser control.
The example in the SDK has the same behavior.
Has anyone found a work-around for this?
The solution I have found is to use Javascript to change the CSS properties for the element:
private void FacebookLoginBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
// check for when this approve page has been navigated to
if (FacebookLoginBrowser.Source.AbsolutePath == "/connect/uiserver.php")
{
showBrowser();
// do the script injection on the LoadCompleted event - doing it here will appear to work when you have a fast connection, but almost certainly fails over 3G because the elements aren't ready in time to be modified
FacebookLoginBrowser.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(FacebookLoginBrowser_LoadCompleted);
}
// etc ...
}
void FacebookLoginBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
FacebookLoginBrowser.LoadCompleted -= FacebookLoginBrowser_LoadCompleted;
// Facebook will likely change this and break our code soon, so make sure you anticipates this
try
{
FacebookLoginBrowser.InvokeScript("eval", "document.getElementById('platform_dialog_bottom_bar').style.position = 'relative';document.getElementById('platform_dialog_bottom_bar').style.top = '-60px';");
}
catch
{
// TODO: display instruction to scroll down if we ever end up here
}
}
I hope that helps. Feel free to contact me if you run into problems
I haven't tried this, but could you use the WebBrowser control's Scale(SizeF) method to change the zoom level of the page?