joker
joker

Reputation: 3752

accessing websites using C#

I have a problem here. Assume there's a basic calculator implemented in javascript hosted on a website ( I have googled it and to find an example and found this one: http://www.unitsconverter.net/calculator/ ). What I want to do is make a program that opens this website, enters some value and gets the return value. So, in our website calculator, the program: - open the website - enters an operand - enters an operation - enters an operand - retrieve the result Note: things should be done without the need to show anything to the user ( the browser for example ).

I did some search and found about HttpWebRequest and HttpWebRespond. But I think those can be used to post data to the server, which means, The file I'm sending data to must be php, aspx or jsp. But Javascript is client side. So, I think they are kind of useless to me in this case.

Any help?

Update: I have managed to develop the web bot using WebBrowser Control tool ( found in System.Windows.Forms ) Here's a sample of the code:

    webBrowser1.Navigate("LinkOfTheSiteYouWant"); // this will load the page specified in the string. You can add webBrowser1.ScriptErrorsSuppressed = true; to disable the script in a page
    webBrowser1.Document.GetElementById("ElementId").SetAttribute("HTMLattrbute", "valueToBeSet");

Those are the main methods I have used to do what I wanted to. I have found this video useful: http://www.youtube.com/watch?v=5P2KvFN_aLY

Upvotes: 0

Views: 817

Answers (4)

Alberto De Caro
Alberto De Caro

Reputation: 5213

It is a matter of API: "Does the remote website expose any API for the required functionality?".

Well web resources that expose interactive API are called web service. There are tons of examples (Google Maps for istance).

You can access the API -depending on the Terms & Conditions of the service- through a client. The nature of the client depends on the kind of web service you are accessing.

So, if there is an accessible web service called "Calculator", then you can access the service and, for istance, invoke the sum method.

In your example, the calculator is a Javascript implementation, so it is not a web service and it cannot be accessed via HTTP requests. Though, its implementation is still accessible: it is the javascript file where the calculator is implemented. You can always include the file in your website and access its functions via javascript (always mind terms and conditions!!).

A very common example is the jQuery library stored in Google Libraries.

Upvotes: 0

Giovanni B
Giovanni B

Reputation: 1032

What you're looking for is something more akin to a web service. The page you provided doesn't seem like it accepts any data in an HTTP POST and doesn't have any meaningful information in the source that you could scrape. If for example you wanted to programmatically make searches for eBay auctions, you could figure out how to correctly post data to it eg:

http://www.ebay.com/sch/i.html?_nkw=http+for+dummies&_sacat=267&_odkw=http+for+dummies&_osacat=0

and then look through the http response for the information you're looking for. You'd probably need to create a regular expression to match the markup you're looking for like if you wanted to know how many results, you'd search the http response for this bit of markup:

<div class="alt w"><div class="cnt">Your search returned <b>0 items.</b></div></div>

As far as clientside/javascript stuff, you just plain aren't going to be able to do anything like what you're going for.

Upvotes: 0

48klocs
48klocs

Reputation: 6103

I guess you could use something like WatiN to pipe the user's input/output from your app to the website and return the results, but as another commenter pointed out, the value of this sort of thing when you could just write your own calculator fairly escapes me.

Upvotes: 3

kevin628
kevin628

Reputation: 3526

You'll need a JavaScript interpreter (engine) to parse all the JavaScript code on the page.

https://www.google.com/search?q=c%23+javascript+engine

Upvotes: 0

Related Questions