I need to launch a browser, do some work and then make the browser navigate to a URL (in that order).
The first part is of course simple and I have a Process object. I am at a loss as to how to later direct it to the target page?
How do I treat the Process as a browser and make it navigate to the desired page?
Any help, pointers, code snippets appreciated.
Instead of launching the browser & then navigating to the page, just tell the OS that you want to run the URL. Windows will pick the correct browser, and navigate the user to the given URL.
System.Diagnostics.Process.Start("http://www.StackOverflow.com");
If you don't need to do this in production, you could use a testing library such as WatiN to do this:
using WatiN.Core;
//Placeholder page to launch initial browser
IE ie = new IE("http://www.google.com");
DoSomeWork();
//Now navigate to the page you want
ie.GoTo("http://stackoverflow.com");
My first instinct for this question was DDE, but it appears that has been decommissioned in Windows Vista so that is no good. Shame, as it was the only consistent mechanism in Windows for Interprocess Communication (IPC)...oh how I miss Arexx on the Amiga.
Anyhow, I believe the following will work but unfortunately, due to the way it works, it launches Internet Explorer irrespective of the configured browser.
If your application has a Form, then create a WebBrowser control on it. Set this to non-visible as we are only making use of its as a launching device rather than to display the web page.
In code, at the point where you want to show a web page, use the following code:
webBrowser1.DocumentText = "window.open('How to launch a browser and later direct it to a page?', 'BananasAreOhSoYummy');";
What this does is to tell the WebBrowser control, which is just the IE in disguise, to open a new window called 'BananasAreOhSoYummy'. Because we have given the window a name, we can use that line repeatedly, with different URLs, to change the page in that particular browser window. (A new window will be opened if the user has happened to close it.)
I will have a think about an approach that honours the user's default browser choice.
If you don't need the actual instance of IE, you can use the System.Windows.Forms.WebBrowser control.
I think instead of sending the browser a url you could send it javascript that would run and direct the browser to a site.
Not sure if this would work but I see no reason why it wouldn't
Related
I'm writing a desktop app in C# that has Facebook integration and I'm trying to figure out how to do authentication/login. I have thought of two different approaches:
1. Popup default browser
The user probably is logged into Facebook on their default browser.
Code: System.Diagnostics.Process.Start("http://www.facebook.com/...");
Issues/Questions: How do I control the window location and size (e.g. not show address bar when it starts)? Can I destroy the process after login is complete or even close the window (won't most browsers prompt for window closing if done from javascript?)?
2. Popup specific browser
If I lookup the default browser, I can pass command-line flags to the browser. "..\chrome.exe" --app=http://www.facebook.com/...
Questions: How do I set the window size/location? How do I close the process after login-complete (assuming I know when the login is complete)?
Is there a better way to do this?
very usefull resource in this situation is http://facebooksdk.net/.
Earlier, there was a sample project for desktop application on facebooksdk.net, but it was removed. You can see it here https://github.com/MarkAureliy/facebook-winforms-sample-master
This project is straight for your needs
I have this code implemented in my application, but whenever i click onto the link, it do help me open a new window. But the original page was "refresh", it kept go all the way back to the top. How can i resolve this problem?
code:
Response.Write("<script>window.open('" + url + "')</script>");
As I understand it, each time you click the link the page is being send to the server where the event is handled (with some C#). If you do that, the server will send the whole page back.
You probably want to control this on client side, with some Javascript.
That said, what you are problably looking for is the attribute target of the link:
something
That will tell the browser that you want to open another tab or windows when the user click the link, and then there request the page specified by url in that tab or window.
Sounds like you want Response.Redirect(myURL)
When you click on the link and handle it in the code behind, that means the link is run at the server side, so it has to post back, which makes it look like it's "refreshing", but it's actually posting back.
You need to handle the opening of the new window on the client's side via Javascript.
If your're writing this to your page, this will redirect you to the url you want
Response.Write("<script>;location.href='" + url + "'</script>");
I'm trying to download file from FTP using javascript, for which I created the following topic:
Is it possible to download file from FTP using Javascript?
From there I learned that I can use window.open('ftp://xyz.org/file.zip'); to download the file. It opens a browser new window, but the window closes immediately.
How I can I force it to stay open?
Actually I do all these in Silverlight application:
Here is the code:
HtmlPage.Window.Eval("window.open('" + url+ "', 'Download', 'height=500,width=800,top=10,left=10');");
I also tried this,
string targetFeatures = "height=500,width=800,top=10,left=10";
HtmlPage.Window.Navigate(new Uri(url), "_blank", targetFeatures);
But both results in same : it opens a window, and closes it immediately. I see it just for fraction of second!
I know this doesn't answer your question, and I'm sure you know all of this. I'm answering more because I don't see this point brought up often. :)
Silverlight has very limited support for client interactions. Javascript is a shim that in my opinion gets overused to try and bypass things that Silverlight was architectured against. It would have been very easy for Microsoft to include FTP support in Silverlight but it was excluded for a reason.
However, Silverlight has great support for webservice interactions. So the recommended way of getting a file would be to call a webservice that would do the FTP transfer for you and then send the contents down to the Silverlight application via the webservice. Possibly even processing it on the webservice side for any business logic etc.
Like I said, I suspect your requirement is to not use a webservice (to pass the bandwith cost onto the user most likely). But it'd be interesting to know more about your business problem instead of your technical problem for the solution you've chosen.
It closes because it triggers file download. You can open two windows - one for message and one to download file, but I thiunk user will know it is downloading...
If I were you, I'd open up a page that has whatever visual/UI stuff you'd want to show the user, and either have a META tag that redirects to the download URL, or has a javascript blurb to fire off said download. That way, your window will stay open, but the download will still start automatically.
to keep it open use
var test = window.open();
test.location = 'ftp://openbsd.org.ar/pub/OpenBSD/2.0/arc/kernels/bsd.ecoff';
and to not open any window use
window.location = 'ftp://openbsd.org.ar/pub/OpenBSD/2.0/arc/kernels/bsd.ecoff';
or make a normal link
Remember that a browser is not meant to "display" (visually anyway) the FTP protocol, and not all browsers will suport it. If you want to allow the user to download something, consider using a normal http:// protocol, and opening a window normally as others have suggested.
If you really need the download to be hosted via FTP, consider your backend ingesting (and caching) the file and return it to the user via http
There is nothing to be parsed on the browser's side, hence it closes. If you want to have the page open, you'll have todo something dirty. Like creating a html (or php) page and serve the content you want the user to see, then with a hidden i-frame which will call the FTP contents.
This way your user will see the content you want them to see, and the file is being downloaded.
I had the exact same problem, Silverlight opening a new window for downloading a file would flash a blank window up briefly and it would disappear again without the file download occurring.
This seemed to happen in IE 8 (not 9 and up) and could be fixed by going into Tools->Internet Options->Security then click Custom level... (for whatever zone your site would be in) and go to Downloads->Automatic prompting for file downloads and make sure this is Enabled (I also have File download enabled below that). This Automatic prompting for file downloads setting seems to be absent from IE 9+.
Another workaround is to not open in a new window, if the target url immediately downloads a file it won't change the current window so there's no difference in UX:
HtmlPage.Window.Navigate(new Uri("\download.ashx?fileid=12345"));
I've application that uses another web sites data so how can i get it because it uses some JavaScript functions to get that data and it not show in page view-source.
Check the NET tab in firebug, XHR and check the resource that is requested, and request the same resource.
Basically you have to render the webpage and ensure the javascript functions are run (evaluated). You could do this by "borrowing" their javascript files (by linking to them from your own page), but this may not work as you don't know what's in those files - they could be accessing DOM elements that you don't have in your page, or calling to other domains which may prevent them from working correctly.
The easiest way to show the same data is to just host the page inside an iframe on your own page. If you are looking to do this from a normal client application (i.e. not a web app) then you will need a browser control that you navigate to the target page. If the browser control is invisible you could then scrape values from it and show them in your app, although this is a very clumsy way to do it, and it's debatable about how ethical it is.
If you want the another web site view source use the HTTPWebRequest to get the response stream in c#.
I know there is built-in Internet explorer, but what I'm looking for is to open Firefox/Mozilla window (run the application) with specified URL. Anyone can tell me how to do that in C# (.nET) ?
You can do this:
System.Diagnostics.Process.Start("firefox.exe", "http://www.google.com");
This will launch the system defined default browser:
string url = "http://stackoverflow.com/";
System.Diagnostics.Process.Start(url);
Remember that Process.Start(url) might throw exceptions if the browser is not configured correctly.
See ProcessInfo.UseShellExecute
Use the Process class (System.Diagnostics) using the URL as the process name. This will use the system default browser to open the URL. If you specify a browser, you run the risk that the browser doesn't exist.
In Visual Studio click the File -> Browse With... on the menus and then select the browser that you want to use. You can also change the browser there. If the Browse With... menu option doesn't appear then you need to select a project from your solution that that can be launched in a browser.
If you explicitly do not want to use the User's default browser, you can run the browser with the URL as the first argument.
C:\Program Files\Mozilla Firefox>firefox.exe http://google.com
launches Firefox with google for me. But as people have said, you run the risk of it not being installed, or being installed to a different place, etc.