Omar Kooheji
Omar Kooheji

Reputation: 55800

Listening for http requests in a windows forms application

I've got to write a .net windows forms application that will open a webpage and then be able to react to the user clicking on certain links on the webpage. The specification I've been given has the links on the webpage just being http links.

Is there a way for my .net application to have a minimal web server on it which will allow it to handle http requests on a given port?

Upvotes: 1

Views: 7173

Answers (6)

Digvijay
Digvijay

Reputation: 361

Please look at the WebBrowser control and specifically the "ObjectForScripting" property. If you set it to the parent form you can actually handle javascript events from the page loaded in the webbrowser in your c# code!!!

I hope that helps!

Upvotes: 1

Steven P
Steven P

Reputation: 1996

If all you need is to show a webpage, and you don't have any restrictions on the browser used, then the WebBrowser control will do the trick.

  1. Drag it on to your form
  2. Set the Url property to the page you need to display
  3. Attach to the Navigating event

You can now respond to clicks, cancel them, do whatever you like. If it's just responding to client-side clicks you need, you don't need a web server. If you DO need a webserver, WinForms shouldn't have anything to do with it.

webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);

private void webBrowser1_Navigating(object sender, 
    WebBrowserNavigatingEventArgs e)
{
   //Do your thing... maybe set e.Cancel if you don't to navigate
}

Upvotes: 1

Fen
Fen

Reputation: 943

I have interpreted the question differently to other users, so maybe I am way off but, I read it as he is trying to render web pages from the web and react to a user clicking on a link within the web page.

The only way I can think of doing this is by using some form of renderer ie webkit and hooking into that to intercept the clicks a user makes.

Upvotes: 0

Joe Enzminger
Joe Enzminger

Reputation: 11190

There are different ways to do this depending on what functionality you need. If all you need to do is respond to click events, and you don't need "full" http protocol support, you can just open a socket and parse what comes in from the browser.

Alternatively, you can use HttpListener, which takes care of the http protocol parsing for you and is relatively easy to use. For what I think you need, this is probably the preferred approach. Simple, non-compiling example here: https://gist.github.com/1770645.

The "holy grail" is hosting the ASP.NET runtime in your windows forms application. I've done this and it is pretty involved. The runtime has to be hosted in a separate AppDomain, so you end up jumping through a lot of hoops to get everything running and hooked up. It also involves writing an implementation of HttpWorkerRequest that is more full featured that the framework provided SimpleWorkerRequest. Incidentally, this also works for windows services, which gives you a great way to provide service management and monitoring through a browser without having a dependency on IIS.

Upvotes: 0

Related Questions