"Original" Web Capture package HttpWebRequest does not return JavaScript-generated cookie Solutions

Source: Internet
Author: User

Objective:

Recently in the process of doing China Mobile Crawler, the first encounter is in a request, there is a "WT_PFC" cookie key value is generated by the front-end JavaScript, not into the HttpWebResponse, that is, C # does not go back to execute client script, HttpWebRequest is not a true web browser, it only downloads HTML information for the address it requests, and it never executes JavaScript or Ajax.

However, because the request of other requests need to sent the cookie, so I looked up a lot of information, basically can only reconstruct JS algorithm or use WebBrowser automatically to execute page JS, but these are not the best and fastest way. I'm using the following

C # code dynamically compiles JavaScript code to derive the return value after the JavaScript function is called.

1.Cookie (WT_FPC):

2. The JS code that generated the cookie is found through HttpWatch:

public static function getwt_fpc () {         var $t = "2";    var $u = new Date ();    var $v = new Date ($u. GetTime () + 315360000000);    var $w = new Date ($u. GetTime ());         if ($t. length <) {         var $x = $u. GetTime (). toString ();        for (var i = 2; I <= (32-$x. length); i++) $t + = Math.floor (Math.random () * 16.0). toString (+);        $t + = $x;    };    $t = encodeURIComponent ($t);     Return "wt_fpc=id=" + $t + ": lv=" + $u. GetTime (). ToString () + ": ss=" + $w. GetTime (). ToString ();    };

Note: Please write the script function in the format above, that is, add "public static".

3.JsHelper (dynamically compiled JS code):

I put the above JS code in the local "wt_fpc.js" file

public static class Jshelper {//<summary>///Execute JS method//</summary>///<param name= "MethodName" > method name </param>///<param name= "para" > Parameters </param>///<returns></returns>public static string GETJSMETHD (String methodName, object[] para) {string path = AppDomain.CurrentDomain.BaseDirectory + "Wt_fpc.js"; string str2 = File.readalltext (path); StringBuilder sb = new StringBuilder (); sb.    Append ("Package aa{"); Sb. Append ("public class JScript {"); sb. Append (STR2); sb.  Append ("}}"); CompilerParameters parameters = new CompilerParameters ();p arameters. GenerateInMemory = true; CodeDomProvider _provider = new Microsoft.JScript.JScriptCodeProvider (); CompilerResults results = _provider.compileassemblyfromsource (parameters, sb.) ToString ()); Assembly Assembly = results.compiledassembly; Type _evaluatetype = assembly. GetType ("AA. JScript "), Object obj = _evaluatetype.invokemember (" GETWT_FPC ", bindingflags.invokemethod,null, NULL, para); return obj . ToString ();}}

Note: If the above helper code error, 99% is due to the JS code problem, that is, JS code is not standard or variable missing definition.

4. C # code calls helper to get execution results

Set the cookie "WT_FPC" string wt_fpc = Jshelper.getjsmethd ("GETWT_FPC", null); Cookiecollection HCC = new cookiecollection (); Cookie Wtcookie = new Cookie () {Expires = DateTime.Now.AddYears (ten), Path = "/", Domain = ". 10086.cn", Name = "WT_FPC",  V Alue = wt_fpc. Substring (WT_FPC. IndexOf (' = ') + 1, wt_fpc. LENGTH-7)//};HCC. ADD (Wtcookie); HTTPHELPERNEW.COOKIE.ADD (Wtcookie);

  

5. End! Small experience: sometimes JavaScript front-end generated cookie, sometimes server-side is not verified, that is, if the cookie value is not through the JS code dynamic, write directly to the dead should also be able.

"Original" Web Capture package HttpWebRequest does not return JavaScript-generated cookie Solutions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.