C# and JavaScript.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adm1n
    New Member
    • Aug 2009
    • 5

    C# and JavaScript.

    Hi all,

    Sometime ago I wrote some automation tool to get content of some site.
    Everything worked fine untill one day...

    My C# code creted url string and made http hit. I used HttpRequest and Response.
    Request looked like http://somesite.con/param1=a&param2 =b&param3=c
    Now, there was a change in the site and it always makes redirect to the url that looks like:
    http://somesite.con/param1=a&param2 =b&param3=c&rnd =AbCdEfG1.
    With the fiddler I saw that actially web browser makes 2 hits:
    1. The old one.
    2. The new one.
    Now the response of the first hit contains some JS code that generates this random code (AbCdEfG1), concatinates it to the main url and makes redirect.

    As far as I understand I need to execute this JS from C#. It's may be done in the several ways and seems not too complex to do.
    But then I need to or:
    a. get back this random value from JS.
    b. allow to JS to make redirection, but need to get back the response.
    I prefer the b. because code that generates this random value always different (code generation or something).

    The code of JS is something like that:
    Code:
    html>
        <head>
            <title></title>
        </head>
    
    
        <script type="text/javascript">
            function redirect()
            {
                var redirectLoc = window.location.href;
                if (redirectLoc.indexOf ('?')>-1) redirectLoc +="&"; else redirectLoc+="?";
                redirectLoc = redirectLoc.replace (/rnd=[0-9a-zA-Z]+&/,"");
                window.location = redirectLoc+'rnd='+f1();
            }
        </script>
    <script type="text/javascript">
    					function sbbpWvum()
    					{
    						nCc = typeof "zCa";
    						return String.fromCharCode(nCc.charCodeAt(4)^37);
    					}
    function f1()
    					{
    						return sbbpWvum();
    					}
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I don't completely understand your problem.
    What is the random bit of code used for?


    Anyways, there's no way for you to execute JavaScript code in your C# code. JavaScript runs in the web browser, and C# code runs on the web server.

    So, there's no way to execute JavaScript from your C# code...

    What are you really trying to do?

    Comment

    • adm1n
      New Member
      • Aug 2009
      • 5

      #3
      Originally posted by Frinavale
      I don't completely understand your problem.
      What is the random bit of code used for?


      Anyways, there's no way for you to execute JavaScript code in your C# code. JavaScript runs in the web browser, and C# code runs on the web server.

      So, there's no way to execute JavaScript from your C# code....

      What are you really trying to do?
      First of all thanks for reply.


      Then, there is a way to execute JS code from C# by using WebBrowser object and its method InvokeSkript. The problem is when I use this method I can't get reply for future parsing.


      As I said, I'm parsing a content of some site with lots of advertising content.
      Some time ago I just to need to make http hit and get the reply from the server.
      Now server doesn't return the reply as it was before. It makes redirection by adding to url some random string generated by code I showed in the first code.
      The code also generated randomaly, at the time I access the server. And then it redirects my hit with edition that was generated.

      I make hit that looks like this:

      The reply to this hit contains the code you can see in the first post.
      The code adds to my hit &rnd=A (for example) and then makes hit with the new url (http://somesite.com/param1=123&rnd=A).

      Hope it clears the question.
      Thanks.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Sorry, for some reason (don't know why) I thought you were developing a C# web application...n ot a desktop application that's connecting to a website. I can't help you with this because I have no experience working with the tools you're working.

        Best of luck,
        -Frinny

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Is that the actual js code thats running? Because then it shouldn't matter what the random values are, you could make them up in your c# code and bypass it entirely?
          If not, then I rather guess that the javascript is actually pulling the session value from the cookie list and appending it to the url.

          Comment

          • adm1n
            New Member
            • Aug 2009
            • 5

            #6
            Frinavale and Plater, thanks for replies.

            Plater,
            Yes, this is actual code that is running, but functions that generate random string generated each time the page is accessed, in current example it is "function sbbpWvum()".

            I solved it in the following way, may be very foolish, but I'm not strong in web development at all:

            1.I save html code that generates the string locally i html file.
            2.Replace "window.locatio n = redirectLoc+'rn d='+f1();" by "document.t itle = redirectLoc+'rn d='+f1();"
            3.Declare WebBrowser control with this local html as uri.
            4.Wait until the page executed (using DocumentComplet ed event)
            5.And finally, I got the desired value in WebBrowser.Docu ment.Title.

            Can you suggest another way? More elegant one, which not required disk access for this temporary html file?

            Thanks.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              The function itself seems silly( I confess I read it wrong the first time)
              Looking at sbbpWvum():
              The line: nCc = typeof "zCa";
              Makes nCc be the string "string"
              The part: nCc.charCodeAt( 4) picks out the character at the 4th index ("n") and gives its ascii value: 110
              110^37produces a very large number, thenString.from CharCode() always produces a "K" for me.

              Unsure what that function should really do, very curious

              Comment

              • adm1n
                New Member
                • Aug 2009
                • 5

                #8
                You are 100% right!
                Those functions really silly. And this function really produces "K".

                The problem is that each time I make a hit to desired web page it redirects me to the page with set of such functions,

                Number of functions and their content generated each time randomly.
                After this page, that main pager redirected me to completed execution of this function set (each function generates 1 character) it builds a string from those chars and this string passed within cookies and added to url.

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  Yuck, this sounds aweful.

                  Comment

                  • adm1n
                    New Member
                    • Aug 2009
                    • 5

                    #10
                    Frinavale, what exactly? :)

                    Comment

                    • Plater
                      Recognized Expert Expert
                      • Apr 2007
                      • 7872

                      #11
                      Originally posted by adm1n
                      Number of functions and their content generated each time randomly.
                      After this page, that main pager redirected me to completed execution of this function set (each function generates 1 character) it builds a string from those chars and this string passed within cookies and added to url.
                      Wow that is awful. I hope whatever site that is suffers for its own abuse (making a full call everytime to add ONE character to the string is silly).
                      Not sure what to tell you on this one unfortunatly.

                      Comment

                      • JamieHowarth0
                        Recognized Expert Contributor
                        • May 2007
                        • 537

                        #12
                        My bets are that the site admins have worked out they're being crawled regularly and have put that code in for two reasons:
                        1) Cache prevention - so you have to crawl a "separate" URL every time which causes a browser to load the page "fresh" rather than from the temporary Internet cache;
                        2) Crawl protection. By throwing a spanner in the works they're hoping you don't crawl the site.

                        Does the content you want to crawl load without the additional querystring parameter they've added?

                        The other option you have is spawning a new browser window using code, letting the browser handle the JS code, then grab the new URL and parse that back into your app, then disposing of the browser window. This is how Excel file parsing was done the old-fashioned way in classic ASP and it's do-able but definitely an undesirable way of programming in .NET.

                        Hope this helps.

                        codegecko

                        Comment

                        Working...