views:

375

answers:

3

As some of you may know, you are able to POST with C#. This means you can "push" buttons on a website with webrequest/response. Now there are also buttons on sites which work with javascript, they start like:

(function($j){
$j.data(document, 'maxPictureSize', 764327);
share_init();
})(jQuery.noConflict());

Is there any solution you can make those function calls in C# with like httprequests or any other kind of library?

+1  A: 

Even when using JavaScript to do a POST there is a POST somewhere in the JS which works the same way as button submit. You just have to dig to the place where the JS code posts and see how it does it. Then craft the same post in C#.

Take for example ASP.NET's own __doPostBack function

var theForm = document.forms['aspnetForm'];
if (!theForm) {
    theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}

You can see that it digs for the form sets several values for input fields and does a submit. Basically you need to fill the same values for the inputs and submit the same form and you've got the JS submit done yourself.

Stilgar
But in the example above you see the only javascript (there is also an line that executes this though) but how could I post that value above with my own value inserted?
Julian
the example above posts with JQuery. You have to dig in in the JQuery code to see what it posts exactly. I believe it will be easier if you just use Fiddler or Firebug to intercept the POST request itself and see what data is posted.
Stilgar
A: 

You need to capture the requests and headers those buttons are sending and simulate them with HttpWebRequest. You could also take a look at WatiN if you want to automate user actions on web sites.

Darin Dimitrov
Had a quick look, but as far as I see I can't see how he POSTS javascript data.
Julian
It does not POST javascript data. It simulates button clicks.
Darin Dimitrov
+1  A: 

I'm assuming you have a program that wants to manipulate the server "back end" for a web page by making the server think that someone pushed a button that POSTs, and sending the data that the web page would include with its POST.

The first tool you need is Microsoft Network Monitor 3.3, or another network packet tracing tool. Use this to look at the POST from the real web page. NetMon (at least) decomposes the packet into the HTTP pieces and headers, so you can easily see what's going on.

Now you will know what data the real POST is sending, and the URL to which it is sending the data (with any possible "query string" - which is unusual for a POST).

Next you need to write C# to create the same sort of POST to the same URL. It seems that you already know about HttpWebRequest/HttpWebResponse so I won't explain them in detail. You may have noticed in your NetMon trace that the Content-Type header was application/x-www-form-urlencoded. This is most often data from an HTML form which is URL-Encoded (like the name), so you need to URL-Encode your data before POSTing it, and you need to know the size of the encoded data for the Content-Length. HttpUtility.UrlEncode() is one method to use for this encoding.

Once you think you have it, try it and use NetMon to inspect your POST request and the response from the server. Keep going until you have duplicated what the mystery web page is doing.

Bob Denny
Thanks! Your post really helped me, going to look at the program and test it.
Julian
Oh little last question, when I push a javascript button it will also provide a post at the end of the script right? So I can see what the javascript posts?
Julian
Anytime you do anything on the browser that results in a POST going to the server, the packet tracer sill record it. HOW it gets produced within the browser's code is not so interesting. It's WHAT gets sent to the server and WHAT the server sends back that is important if you want to reproduce it in your program.
Bob Denny