473,385 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C# and JavaScript.

5
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:
Expand|Select|Wrap|Line Numbers
  1. html>
  2.     <head>
  3.         <title></title>
  4.     </head>
  5.  
  6.  
  7.     <script type="text/javascript">
  8.         function redirect()
  9.         {
  10.             var redirectLoc = window.location.href;
  11.             if (redirectLoc.indexOf ('?')>-1) redirectLoc +="&"; else redirectLoc+="?";
  12.             redirectLoc = redirectLoc.replace (/rnd=[0-9a-zA-Z]+&/,"");
  13.             window.location = redirectLoc+'rnd='+f1();
  14.         }
  15.     </script>
  16. <script type="text/javascript">
  17.                     function sbbpWvum()
  18.                     {
  19.                         nCc = typeof "zCa";
  20.                         return String.fromCharCode(nCc.charCodeAt(4)^37);
  21.                     }
  22. function f1()
  23.                     {
  24.                         return sbbpWvum();
  25.                     }
  26.  
  27.  
Aug 17 '09 #1
11 2367
Frinavale
9,735 Expert Mod 8TB
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?
Aug 17 '09 #2
adm1n
5
@Frinavale
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:
http://somesite.com/param1=123
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.
Aug 18 '09 #3
Frinavale
9,735 Expert Mod 8TB
Sorry, for some reason (don't know why) I thought you were developing a C# web application...not 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
Sep 3 '09 #4
Plater
7,872 Expert 4TB
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.
Sep 4 '09 #5
adm1n
5
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.location = redirectLoc+'rnd='+f1();" by "document.title = redirectLoc+'rnd='+f1();"
3.Declare WebBrowser control with this local html as uri.
4.Wait until the page executed (using DocumentCompleted event)
5.And finally, I got the desired value in WebBrowser.Document.Title.

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

Thanks.
Sep 6 '09 #6
Plater
7,872 Expert 4TB
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.fromCharCode() always produces a "K" for me.

Unsure what that function should really do, very curious
Sep 8 '09 #7
adm1n
5
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.
Sep 9 '09 #8
Frinavale
9,735 Expert Mod 8TB
Yuck, this sounds aweful.
Sep 9 '09 #9
adm1n
5
Frinavale, what exactly? :)
Sep 10 '09 #10
Plater
7,872 Expert 4TB
@adm1n
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.
Sep 10 '09 #11
JamieHowarth0
533 Expert 512MB
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
Sep 10 '09 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.