【WinForm】C# 采用POST登錄京東


C# POST 傳值登錄 京東

  想做一個DEMO 練練html的傳值和接收,就用Winform 做了一個登錄京東的程序。

  首先參考的網址是:

 艹蛋的青春じ 讓我蛋疼ミ:http://www.cnblogs.com/lvxiaojia/p/3292689.html

                     螳螂蝦:   http://www.tanglangxia.com/archives/3812.html

   然后要做登錄,肯定是要抓取數據,分析數據,然后通過后台模擬,get和post所需要的值即可,這個是大致思路。

一、抓取數據

   這里采用的工具是Fiddler2(點擊下載),進行數據采集分析的。

   抓取的大概界面如下:

 

二、分析數據

 

uuid=ffc0df62-3bc3-4c12-a654-15676dacf2c4&loginname=zhanghu&nloginpwd=123&loginpwd=123&machineNet=&machineCpu=&machineDisk=&authcode=&qASPhlpNIq=BGWkk

   解析PostData 我們可以發現 需要發送的值有:

   ①uuid  -->uuid是個頁面中隱藏的ID如截圖

   ② loginname --> 即賬戶名

   ③ nloginpwd 和loginpwd --> 登錄密碼

   ④ qASPhlpNIq 和BGWkk  -->這兩個是個隨機變量的后綴 不管是name 還是 value 但是在頁面的位置如截圖所示

 

三、后台模擬

   ①     獲取數據(GET)

   分析了數據我們就要想辦法獲取到所需要的變量,首先,要我采用的方式是在程序預加載的時候獲取到除賬戶和密碼外的變量,具體代碼如下

//存儲uuid

        string uuid = "";

        //存儲提交后綴

        string lastName = "";

        string lastValue = "";

private void Form1_Load(object sender, EventArgs e)

        {

HttpItem item = new HttpItem();

            HttpHelper helper = new HttpHelper();

            HttpResult result = new HttpResult();

 

            item.Method = "GET";

            item.URL = "http://passport.jd.com/new/login.aspx?returnUrl=http%3A%2F%2Fvip.jd.com%2F";

            result = helper.GetHtml(item);

            cookies = result.Cookie;

            string htmltext = result.Html;

            var ary = Regex.Matches(htmltext, @"(?is)<input(?=[^>]*?name=[""'](?<name>[^""'\s]+)[""'])(?=[^>]*?value=[""'](?<value>[^""'\s]+)[""'])[^>]+>").OfType<Match>().Select(t => new { name = t.Groups["name"].Value, value = t.Groups["value"].Value }).ToArray();

            uuid = ary.ToList()[0].value;

            lastName = ary.ToList()[1].name;

            lastValue = ary.ToList()[1].value;

}
View Code

   ②     頁面傳值(POST)

   有了數據 就可以向頁面提交了、

/*登錄*/

        private void btnLogi_Click(object sender, EventArgs e)

        {

            HttpItem item = new HttpItem();

            HttpHelper helper = new HttpHelper();

            HttpResult result = new HttpResult();

 

            item.URL = "http://passport.jd.com/uc/loginService?uuid=" + uuid + "&ReturnUrl=http%3A%2F%2Fvip.jd.com%2F&r=0.3457543252407181";

            item.Method = "POST";

            item.Allowautoredirect = true;

            item.ContentType = "application/x-www-form-urlencoded";

            item.Postdata = "uuid=" + uuid + "&loginname=" + txtUser.Text.Trim() + "&loginpwd=" + txtPwd.Text.Trim() + "&machineNet=&machineCpu=&machineDisk=&authcode=&"+lastName+"="+lastValue+"";

            item.Header.Add("x-requested-with", "XMLHttpRequest");

            item.Header.Add("Accept-Encoding", "gzip, deflate");

            item.Referer = "http://passport.jd.com/new/login.aspx?ReturnUrl=http%3A%2F%2Fvip.jd.com%2F";

            item.Accept = "*/*";

            item.Encoding = Encoding.UTF8;

            item.Cookie = cookies;

            result = helper.GetHtml(item);

            cookies ="__jda=95931165.290243407.1371634814.1371634814.1371634814.1; __jdb=95931165.1.290243407|1.1371634814; __jdc=95931165; __jdv=95931165|direct|-|none|-;" + result.Cookie;

            cookies = cookies.Replace("HttpOnly,", null);

            txtResult.Text += ("登陸成功了!\n" + result.Html);

 

        }

 
View Code

四、總結

    這個小程序DEMO,本來挺簡單的一事,但是首先遇到的就是剛開始的時候,分析數據的時候,不全面,沒有把傳值的最后的后綴看成一個變量,導致登錄結果一直是: ({"username":"\u8bf7\u5237\u65b0\u9875\u9762\u540e\u91cd\u65b0\u63d0\u4ea4"}) ,正確的返回結果應該是 ({"success":"http://vip.jd.com/"}) 。其解決方法就是不要用webbrower來獲取uuid和后綴 而是用GET的方式獲取。 最后,特別要感謝那些幫助我的人,謝謝。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM