user392810
user392810

Reputation: 21

Getting data from a webpage

I have an idea for an App that would really help me out in work but I'm not sure if it's possible.

I want to run a C# desktop application that will ask for a value. When a value is supplied, the application will open a browswer, go to a webpage and add the value into a form on an online website. The form is then submitted and a new page is loaded that contains a table of results. I then want to extract the table of results from the page source and write code to parse the result values.

It is not important that the user see's this happen in an actual browser. In other words if there's a way to do it by reading HTTP requests then thats great.

The biggest problem I have is getting the values into the form and then retrieving the page source after the form is submitted and the next page loads.

Any help really appreciated.

Thanks

Upvotes: 2

Views: 1578

Answers (5)

Brian R. Bondy
Brian R. Bondy

Reputation: 347196

This is definitely possible and you don't need to use an actual web browser for this. You can simply use a System.Net.WebClient to send your HTTP request and get an HTTP response.

I suggest to use wireshark (or you can use Firefox + Firebug) it allows you to see HTTP requests and responses. By looking at the HTTP traffic you can see exactly how you should pass your HTTP request and which parameters you should be setting.

Upvotes: 1

MrFox
MrFox

Reputation: 5106

Why would you do this through web pages if you don't even want the user to do anything? Web pages are purely for interaction with users, if you simply want data transfer, use WCF.

@Brian using Wireshark will result in a very angry network manager, make sure you are actually allowed to use it.

Upvotes: 0

Pontus Gagge
Pontus Gagge

Reputation: 17258

See e.g. this question for some pointers on at least the data retrieval side. You're going to know a lot more about the http protocol before you're done with this...

Upvotes: 0

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

Provided that you're only using this in a legal context:

Usually, web forms are sent via POST request to the web server, specifically some script that handles it. You can look at the HTML code for the form's page and find out the destination for the form (form's action).

You can then use a HttpWebRequest in C# to "pretend you are the form", sending a POST request with all the required parameters (adding them to the HTTP header).

As a result you will get the source code of the destination page as it would be sent to the browser. You can parse this.

Upvotes: 1

spender
spender

Reputation: 120380

You don't need to involve the browser with this. WebClient should do all that you require. You'll need to see what's actually being posted when you submit the form with the browser, and then you should be able to make a POST request using the WebClient and retrieve the resulting page as a string.

The docs for the WebClient constructor have a nice example.

Upvotes: 0

Related Questions