I have a .NET desktop application (not web) with a WebBrowser control.
I cannot find any information on how, or if it is even possible, to obtain the HTTP status code when a document is navigated to inside this control. Does anyone know if this is possible or how?
The purpose is to detect codes other than 200 and perform actions accordingly within the application.
A web page is not made up from a single HTTP GET request. The stackoverflow.com front page for example requires 16 requests. Stuff like javascript code, images, page visit counters, coming from different web sites as well. Some of it retrieve from cache instead of downloaded.
WebBrowser (aka Internet Explorer) doesn't support enumerating these individual requests. You'd have to use the HttpWebRequest class, but that of course doesn't make a web page.
Related
I've been searching for hours and couldn't find any solution to this problem.
I'm developing a UWP App and I have a WebView that goes to a website (where the user authenticates) and I should be able to get the access_token after he logs in.
Is there a way to get the response header from the page?
Or do I have to do everything manually (create the HttpClient, send the POST with the login info, and get the header response that way?)
First and foremost, the latest guidance is that authentication should not be done inside a web view, the modern approach is to open external browser window, where the user authenticates and is then redirected back to the app using a custom URI scheme. See a detailed post on this here on SO.
Now, the unfortunate answer is that WebView does not offer a built-in way to access the HTTP response and its headers. This has been requested (see for example this blog post by Martin Suchan), but was not implemented so far. If you have control over the web page, then you could store the authentication info in cookies, which are accessible. Not even injecting custom JavaScript can help here, because getting the HTTP headers is possible if you initiate an AJAX request in JS, but you can't get headers for a page that is already loaded.
As mentioned in comments above, the better solution would be to code the login manually using HttpClient or see if the service support a proper OAuth2/OpenID Connect flow in which case you could use a library like IdentityModel.OidcClient2 which can handle most of the heavy lifting for you.
You can also use the built-in WebAuthenticationBroker, see docs here.
I have a WPF application that redirects to a payment system and then starts pooling a database to see if the transaction reference was posted back via a different channel.
I had a chat with one of my programmer friends and he said that there was no need for pooling and I could have simply tracked Url to which payment system redirects after successful payment and react accordingly.
Code to open window and redirect agent to payments system is as follows:
var process = Process.Start(new ProcessStartInfo(url));
Is there a way to get the Url of the Browser Window?
I would recommend to use a web browser control inside your application. There are very good ones and a built-in version (called WebBrowser). Process.Start is a problem since you never know which browser (and version) is loaded. You have support a lot of different ways to get the URL.
The benefit of using a web browser control in your application is that you have absolute control of the web browser, you can handle events like loading of pages, which enables you to perform checks on the URL. I use this myself to do OAuth authentication on a client and get the token back from the URL and parse the token out of it.
I have a basic C# HttpWebRequest. My problem is the page that it is sending the GET request to, requires javascript (on the client-side) to be enabled for the content to generate.
How can I add javascript support to my code? Is it even possible?
The server can't really know whether the client supports Javascript. It can only go on the data you give it.
So there are two possibilities:
It's using headers to work out what response to send, and inferring that you can't run Javascript. Solution: work out what headers it requires, and set them explicitly.
It's sending you back the page, but you're unable to use it because you're not displaying it on a browser. Solution: look at the page, work out what AJAX calls the Javascript is making, and make those instead. You may not even need to fetch the original hosting page.
HttpWebRequest just implements GET, you need full browser to execute JavaScript (and possibly need CSS files to as scripts may depend on them).
The built in approach is to use WebBrowser control to render pages, that grab innerHTML after you find that JavaScript rendering is done.
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?
Use an HttpListener.
http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx
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.
Drag it on to your form
Set the Url property to the page you need to display
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
}
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!
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.
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.
You can use Nancy
Site of project: https://www.codeproject.com/Articles/694907/Embed-a-web-server-in-a-windows-service
Is there a way using either C# or a scripting language, such as Python, to load up a website in the user's default webbrowser and continue to interact it via code (e.g. invoke existing Javascript methods)? When using WinForms, you can host a Webbrowser control and invoke scripts from there, but only IE is supported. Is there a way of doing the same thing in the user's default browser (not necessarily using WinForms)?
Update: The website is stored on the user's machine, not served from a third party server. It is a help page which works dynamically with my C# program. When the user interacts with my C# program, I want to be able to execute the Javascript methods on the website.
You might want to look into Selenium. It can automate interaction with FireFox, IE, Chrome (with chromedriver) and Opera. It may not be suitable for your purposes due to the fact that it uses a fresh, stripped down profile, rather than the user's normal browser profile.
If you look at the HTTP request header you can determine the user-agent making the request. Based upon that information you can write logic from the server side as to respond with a unique page per detected user-agent string. Then you add any unique JavaScript you want as a static string to be executed by the user-agent application.