【WinForm】C# 采用POST登录京东

时间:2023-03-08 16:35:49

C# POST 传值登录 京东

想做一个DEMO 练练html的传值和接收,就用Winform 做了一个登录京东的程序。

首先参考的网址是:

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

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

然后要做登录,肯定是要抓取数据,分析数据,然后通过后台模拟,get和post所需要的值即可,这个是大致思路。

一、抓取数据

这里采用的工具是Fiddler2(点击下载),进行数据采集分析的。

抓取的大概界面如下:

【WinForm】C# 采用POST登录京东

二、分析数据

【WinForm】C# 采用POST登录京东

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()[].value;

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

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

}

②     页面传值(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);

        }

四、总结

这个小程序DEMO,本来挺简单的一事,但是首先遇到的就是刚开始的时候,分析数据的时候,不全面,没有把传值的最后的后缀看成一个变量,导致登录结果一直是: ({"username":"\u8bf7\u5237\u65b0\u9875\u9762\u540e\u91cd\u65b0\u63d0\u4ea4"}) ,正确的返回结果应该是 ({"success":"http://vip.jd.com/"}) 。其解决方法就是不要用webbrower来获取uuid和后缀 而是用GET的方式获取。 最后,特别要感谢那些帮助我的人,谢谢。