1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Question - C# - Executing Ajax/Javascript function using HttpWebRequest/Response

Discussion in 'C, C++, C#' started by crepito, Jul 22, 2010.

  1. crepito

    crepito Junior Member

    Joined:
    Oct 5, 2008
    Messages:
    145
    Likes Received:
    28
    Location:
    Portugal
    Hey guys,

    Any of you know a solution for executing some ajax/javascript functions returned with the html using HttpWebRequest/Response?

    The problem:
    The page i'm working with has a function that returns a number that must be sent with the "GET" url everytime a new request is done.

    I believe the browser knows this number by executing a javascript funtion

    Code:
    <script type="text/javascript">$(document).ready(function() {
    $("#day, #month, #year").bind("blur", function() { Registration.checkField("date"); });$("#sex").bind("blur", function() { Registration.checkField("sex", $("#sex").val()); });$("#password").passwordchecker({show: "passwordBox"});
    docReady();
    [B]kTrack.mark("r");[/B]
    });</script>
    I've been looking around but so far no results. If any of you already needed this and found a solution please give this fellow coder a little help ;)

    Thanks

    PS- I could easily do this using the WebBrowser component, but i would rather stay away from it and keep it simple/fast using only HttpWebRequest/Response.
     
    Last edited: Jul 22, 2010
  2. crashed

    crashed Jr. VIP Jr. VIP Premium Member

    Joined:
    Aug 13, 2008
    Messages:
    958
    Likes Received:
    1,198
    Occupation:
    Guru-slayer
    Location:
    Behind the VPN...
    Home Page:
    Hmmmmmm, Javascript is a pain and you will have to work out what its doing, and write your own function to compensate.

    As for the Ajax side, that gets even more complicated. I would recommend you get the book HTTP Programming Recipes for C# Bots which has an entire chapter on it!
     
  3. smack

    smack Junior Member

    Joined:
    Feb 1, 2010
    Messages:
    182
    Likes Received:
    78
    Occupation:
    Software Engineer/Evil Genius
    Location:
    inside .NET
    don't worry about executing the js, just send the request in the proper format with the proper information and parse the results from the response.
     
  4. crepito

    crepito Junior Member

    Joined:
    Oct 5, 2008
    Messages:
    145
    Likes Received:
    28
    Location:
    Portugal
    The problem is that i must send the request with the number that is generated with the js function attached to the url. This number changes with every request.

    I think i already figured out how to generate the correct number for each request, my only problem is getting the first one (the base one that will be used to generate all the others for future requests).

    I'll take a look at the receipt book as suggested.
    Thanks for the comments guys.

    Btw, in case someone is wondering, the website i'm having this problem is Kwick.de
     
  5. voyevoda

    voyevoda Regular Member Premium Member

    Joined:
    Mar 21, 2010
    Messages:
    217
    Likes Received:
    97
    Location:
    Eastern Front
    You've never seen eval() used in Javascript?
     
  6. smack

    smack Junior Member

    Joined:
    Feb 1, 2010
    Messages:
    182
    Likes Received:
    78
    Occupation:
    Software Engineer/Evil Genius
    Location:
    inside .NET
    depending on the flavor of ajax they're using sometimes those numbers are completely arbitrary. have you tried just sending junk?
     
    • Thanks Thanks x 1
  7. crepito

    crepito Junior Member

    Joined:
    Oct 5, 2008
    Messages:
    145
    Likes Received:
    28
    Location:
    Portugal
    I'm ashamed to say that i didn't and that was my mistake lol.

    Your suggestion works flawless :p

    Thanks for the tip ;)
     
  8. smack

    smack Junior Member

    Joined:
    Feb 1, 2010
    Messages:
    182
    Likes Received:
    78
    Occupation:
    Software Engineer/Evil Genius
    Location:
    inside .NET
    ah don't be ashamed man, the first time i ran in to that it tripped me up for a bit too.

    which has lead me to devise another one of my rules to live by:

    if at first you don't succeed, try sending junk data. ;)

    glad i could help. happy whoring.
     
  9. sodevrom

    sodevrom Jr. VIP Jr. VIP

    Joined:
    Sep 8, 2010
    Messages:
    2,182
    Likes Received:
    734
    Gender:
    Male
    Occupation:
    IM & BlackHat
    Location:
    011001110110111101100100
    Home Page:
    Hello,
    Well there is a way to do anything you can think off when talking about HTML and webpages.
    Install firebug in Firefox. To activate it, in the bottom right corner you will see a "bug" icon. Click on it.
    After that, navigate to the "problematic" page.
    Next, click on Net(from Dragonfly) and select HTML or XHR (or all... but images will appear also).

    There you will see all requests made to very page. And for every request you will see what POST/GET parameters have been sent + what http headers were used to make the requests.

    From there you can implement everything in your programming language of choice.

    Hope I helped.
    Thanks,
    Vlad
     
  10. smack

    smack Junior Member

    Joined:
    Feb 1, 2010
    Messages:
    182
    Likes Received:
    78
    Occupation:
    Software Engineer/Evil Genius
    Location:
    inside .NET
    i just wanted to post a quick update to this thread with something that i learned recently. it seems that many times when those random or arbitrary numbers are sent with an AJAX post it is a hack to work around some caching issues in IE

    Code:
    http://**************ist.com/2007/05/02/ajax-prototype-scriptaculous-caching-ie-internet-explorer/
    i've often wondered why these numbers show in the requests with seemingly no purpose. well, now i think i know their purpose.

    -edit-

    i don't know why the link was censored, so the domain is: "a r s e n a l i s t"

    usually i don't dodge the filters here, but i think this is an important and good article to read over.
     
    Last edited: Oct 19, 2010
  11. MakeLoot

    MakeLoot Registered Member

    Joined:
    Dec 14, 2010
    Messages:
    52
    Likes Received:
    2
    Very good information that can only come from experience.... a lot of AJAX requests use timestamps to prevent caching.