利用Winform HttpRequest 模拟登陆京东商城

目前只获取订单信息,可以获取图片等其他信息

 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Net;
 using System.Text;

 namespace HelperLib
 {
     public enum ResponeType
     {
         String,
         File
     }
     /// <summary>
     /// HttpRequest Help
     /// Code By:lvxiaojia
     /// blog:http://www.cnblogs.com/lvxiaojia/
     /// </summary>
     public class RequestHelp
     {
         static CookieContainer cookie = new CookieContainer();
         public static string Post(string url, Dictionary<string, string> postData, string referer = "", string accept = "", string contentType = "", ResponeType type = ResponeType.String, string fileSavePath = "", Action<string> action = null, Func<Dictionary<string, string>> fun = null)
         {
             var result = "";
             //var cookie = new CookieContainer();
             StringBuilder strPostData = new StringBuilder();
             if (postData != null)
             {
                 postData.AsQueryable().ToList().ForEach(a =>
                 {
                     strPostData.AppendFormat("{0}={1}&", a.Key, a.Value);
                 });
             }
             byte[] byteArray = Encoding.UTF8.GetBytes(strPostData.ToString().TrimEnd('&'));

             HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);

             webRequest.CookieContainer = cookie;

             webRequest.Method = "POST";
             if (string.IsNullOrEmpty(accept))
                 webRequest.Accept = "application/json, text/javascript, */*;";
             else
                 webRequest.Accept = accept;

             if (!string.IsNullOrEmpty(referer))
                 webRequest.Referer = referer;
             if (string.IsNullOrEmpty(contentType))
                 webRequest.ContentType = "application/x-www-form-urlencoded";
             else
                 webRequest.ContentType = contentType;

             )
                 webRequest.ContentLength = byteArray.Length;

             //请求
             Stream newStream = webRequest.GetRequestStream();
             newStream.Write(byteArray, , byteArray.Length);
             newStream.Close();

             HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
             var responSteam = response.GetResponseStream();

             if (type == ResponeType.String)
             {
                 StreamReader strRespon = new StreamReader(responSteam, Encoding.UTF8);
                 result = strRespon.ReadToEnd();
             }
             else
             {
                 BinaryReader br = new BinaryReader(responSteam);
                 );
                 FileStream fs = new FileStream(fileSavePath, FileMode.OpenOrCreate);
                 fs.Write(byteArr, , byteArr.Length);
                 fs.Dispose();
                 fs.Close();
                 result = "OK";
             }
             if (action != null)
             {
                 action.Invoke(result);
             }
             if (fun != null)
             {
                 Dictionary<string, string> dic = new Dictionary<string, string>();
                 foreach (var item in cookie.GetCookies(webRequest.RequestUri))
                 {
                     var c = item as Cookie;
                     dic.Add(c.Name, c.Value);
                 }
                 fun = () => { return dic; };
             }
             return result;

         }

         public static string Get(string url, Dictionary<string, string> postData=null, string referer = "", Action<string> action = null, Action<Dictionary<string, string>> fun = null)
         {
             var result = "";

             StringBuilder strPostData = new StringBuilder("?");
             if (postData != null)
             {
                 postData.AsQueryable().ToList().ForEach(a =>
                 {
                     strPostData.AppendFormat("{0}={1}&", a.Key, a.Value);
                 });
             }
             )
                 strPostData = strPostData.Clear();
             HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url + strPostData.ToString().TrimEnd('&'));
             webRequest.CookieContainer = cookie;
             webRequest.Method = "GET";
             webRequest.Accept = "text/javascript, text/html, application/xml, text/xml, */*;";
             if (!string.IsNullOrEmpty(referer))
                 webRequest.Referer = referer;
             //请求
             HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
             var responSteam = response.GetResponseStream();

             StreamReader strRespon = new StreamReader(responSteam, Encoding.Default);
             result = strRespon.ReadToEnd();

             if (action != null)
             {
                 action.Invoke(result);
             }
             if (fun != null)
             {
                 Dictionary<string, string> dic = new Dictionary<string, string>();
                 foreach (var item in cookie.GetCookies(webRequest.RequestUri))
                 {
                     var c = item as Cookie;
                     dic.Add(c.Name, c.Value);
                 }
                 fun.Invoke(dic);
             }
             return result;

         }

     }
 }

RequestHelp

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Linq;
 using System.Reflection;
 using System.Security.Cryptography;
 using System.Text;

 namespace HelperLib
 {
     /// <summary>
     ///
     /// </summary>
     public class EncodingHelp
     {
         public static string GetMd5Str32(string str)
         {
             MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
             char[] temp = str.ToCharArray();
             byte[] buf = new byte[temp.Length];
             ; i < temp.Length; i++)
             {
                 buf[i] = (byte)temp[i];
             }
             byte[] data = md5Hasher.ComputeHash(buf);
             StringBuilder sBuilder = new StringBuilder();
             ; i < data.Length; i++)
             {
                 sBuilder.Append(data[i].ToString("x2"));
             }
             return sBuilder.ToString();
         }

         public static string GetEnumDescription(Enum value)
         {
             Type enumType = value.GetType();
             string name = Enum.GetName(enumType, value);
             if (name != null)
             {
                 // 获取枚举字段。
                 FieldInfo fieldInfo = enumType.GetField(name);
                 if (fieldInfo != null)
                 {
                     // 获取描述的属性。
                     DescriptionAttribute attr = Attribute.GetCustomAttribute(fieldInfo,
                         typeof(DescriptionAttribute), false) as DescriptionAttribute;
                     if (attr != null)
                     {
                         return attr.Description;
                     }
                 }
             }
             return null;
         }
     }
 }

EncodingHelp

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Diagnostics;
 using System.Drawing;
 using System.IO;
 using System.Linq;
 using System.Net;
 using System.Text;
 using System.Text.RegularExpressions;
 using System.Web;
 using System.Windows.Forms;
 using HelperLib;

 namespace SimulationSouGouLogion
 {
     public partial class LoginJD : Form
     {
         public LoginJD()
         {
             InitializeComponent();
         }

         static string loginUrl = "https://passport.jd.com/new/login.aspx";
         static string loginServiceUrl = "http://passport.jd.com/uc/loginService";
         static string loginRefererUrl = "http://passport.jd.com/uc/login?ltype=logout";

         private void button1_Click(object sender, EventArgs e)
         {
             richTextBox1.Text = "";

             RequestHelp.Get(loginUrl, null);

             var login = RequestHelp.Post(loginServiceUrl,
                 new Dictionary<string, string>(){
                 {"uuid","59c439c8-09de-4cce-8293-7a296b0c0dd1"},
                 {"loginname",HttpUtility.UrlEncode(tbUserCode.Text)},
                 {"nloginpwd",HttpUtility.UrlEncode(tbPassword.Text)},
                 {"loginpwd",HttpUtility.UrlEncode(tbPassword.Text)},
                 {"machineNet","machineCpu"},
                 {"machineDisk",""},
                 {"authcode",""}}, loginRefererUrl);

             if (!login.Contains("success"))
             {
                 if (login.ToLower().Contains("pwd"))
                 {
                     MessageBox.Show("密码验证不通过!", "提示");
                     tbPassword.Text = "";
                     tbPassword.Focus();
                     return;
                 }
                 else
                 {
                     MessageBox.Show("登陆失败!", "提示"); return;
                 }
             }

             var dic = new Dictionary<string, string>();
             //获取订单列表
             var orderList = RequestHelp.Get("http://order.jd.com/center/list.action?r=635133982534597500", null, fun: a => dic = a);

             //分析网页HTML
             var regexOrder = Repex(@"<tr id=.{1}track\d+.{1} oty=.{1}\d{1,3}.{1}>.*?</tr>?", orderList);

             //获取每个订单信息
             regexOrder.ForEach(a =>
             {
                 var orderCode = Repex(@"<a name=.{1}orderIdLinks.{1} .*? href=.{1}(.*)'?.{1}>(.*)?</a>?", a);
                 ]);
                 richTextBox1.Text += ] + "\r\n";
                 richTextBox1.Text += ] + "\r\n";
             });

             //获取订单信息
             //var details = RequestHelp.Get("http://jd2008.jd.com/jdhome/CancelOrderInfo.aspx?orderid=502561335&PassKey=28C8E5A477E7B255A72A7A67841D5D13");
         }

         List<string> Repex(string parm, string str)
         {
             List<string> list = new List<string>();
             var regex = new Regex(parm, RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.IgnoreCase);
             var math = regex.Matches(str);
             ) return list;

             foreach (var item in math)
             {
                 var strdetails = (item as Match).Value;

                 list.Add(strdetails);
             }

             return list;

         }
         List<string> Match(string parm, string str)
         {
             List<string> list = new List<string>();
             var math = Regex.Matches(str, parm);
             ) return list;
             list.Add(math[].Result("$1"));
             list.Add(math[].Result("$2"));
             return list;

         }

         private void Form1_Load(object sender, EventArgs e)
         {
             tbPassword.Focus();
         }

         private void blogLink_Click(object sender, EventArgs e)
         {
             Process.Start("http://www.cnblogs.com/lvxiaojia/");
         }

     }
 }

窗体后台代码

模拟京东商城登陆HttpRequest的更多相关文章

  1. python 装饰器模拟京东登陆

    要求: 1.三个页面:主页面(home).书店(book).金融页面(finance)2.有两种登陆方式:主页面和书店页面使用京东账户登陆,金融页面使用微信账户登录2.输入:1 ,进入主页面,以此类推 ...

  2. 京东商城跨域设置Cookie实现SSO单点登陆过程

    可以先看下这边文章:http://blog.chinaunix.net/uid-25508399-id-3431705.html   1.点击首页的登陆按钮跳转到京东的登陆中心https://pass ...

  3. ThinkPHP3.2开发仿京东商城项目实战视频教程

    ThinkPHP3.2仿京东商城视频教程实战课程,ThinkPHP3.2开发大型商城项目实战视频 第一天 1.项目说明 2.时间插件.XSS过滤.在线编辑器使用 3.商品的删除 4.商品的修改完成-一 ...

  4. Scrapy实战篇(八)之Scrapy对接selenium爬取京东商城商品数据

    本篇目标:我们以爬取京东商城商品数据为例,展示Scrapy框架对接selenium爬取京东商城商品数据. 背景: 京东商城页面为js动态加载页面,直接使用request请求,无法得到我们想要的商品数据 ...

  5. Android 深入ViewPager补间动画,实现类京东商城首页广告Banner切换效果

    如有转载,请声明出处: 时之沙: http://blog.csdn.net/t12x3456 某天看到京东商城首页的滑动广告的Banner,在流动切换的时候有立体的动画效果,感觉很有意思,然后研究了下 ...

  6. 完美高仿精仿京东商城手机客户端android版源码

    完美高仿精仿京东商城手机客户端android版源码,是从安卓教程网那边转载过来的,这款应用源码非常不错的,也是一个非常优秀的应用源码的,希望能够帮到学习的朋友. _js_op> <igno ...

  7. 商城项目实战 | 1.1 Android 仿京东商城底部布局的选择效果 —— Selector 选择器的实现

    前言 本文为菜鸟窝作者刘婷的连载."商城项目实战"系列来聊聊仿"京东淘宝的购物商城"如何实现. 京东商城的底部布局的选择效果看上去很复杂,其实很简单,这主要是要 ...

  8. 商城项目实战 | 2.1 Android 仿京东商城——自定义 Toolbar (一)

    前言 本文为菜鸟窝作者刘婷的连载."商城项目实战"系列来聊聊仿"京东淘宝的购物商城"如何实现. 现在很多的 APP 里面都有自己的自定义风格,特别是京东商城中自 ...

  9. 商城项目实战 | 2.2 Android 仿京东商城——自定义 Toolbar (二)

    本文为菜鸟窝作者刘婷的连载."商城项目实战"系列来聊聊仿"京东淘宝的购物商城"如何实现. 上一篇文章<商城项目实战 | 2.1 Android 仿京东商城 ...

随机推荐

  1. cookie分析

    浏览器cookie分析 简单点说就是数据存储,通过JavaScript将要保存的数据保存在客户端浏览器,下次打开网页时调用保存的cookie.浏览器中cookie保存的形式:user=aa; pwd= ...

  2. poj3321Apple Tree(树状数组)

    http://poj.org/problem?id=3321 刚一看题以为要建一颗树 看了下讨论说dfs 这里dfs遍历时设的标号很好 一个low一个high 包含了以这一节点为根节点的子树结点的所有 ...

  3. Json.net 同过 JsonConverter 调整导出值(未完成)

    public class TimeSpanConverter : JsonConverter { public override bool CanConvert(Type objectType) { ...

  4. Jquery动画第二部分

    效果图:     →→→→→→ <%@ Page Language="C#" AutoEventWireup="true" CodeFile=" ...

  5. 【转】Cygwin的包管理器:apt-cyg

    原文网址:http://zengrong.net/post/1792.htm Cygwin的包管理工具setup.exe实在是难用的让人蛋碎.于是就有了这样一个apt-cyg,可以提供类似于 apt- ...

  6. 我的Modbus Slave/Client开发历程(Rtu/AscII/Tcp)

    我的Modbus Slave/Client开发历程(Rtu/AscII/Tcp) 分类: [自动化]2007-07-19 10:04 34038人阅读 评论(38) 收藏 举报 vb嵌入式dostcp ...

  7. 无法找到.exe的调试信息

    原文:无法找到.exe的调试信息 前天重装了电脑,并配置了Visual Studio2005的VC正则库,boost/regex,运行速度马上快了三倍吧,到底是怎么快的,我还真说不清楚,因为电脑这玩意 ...

  8. as3+java+mysql(mybatis) 数据自动工具(七) - 完结

    autoscript packed 文件地址:http://pan.baidu.com/s/1dDvgcO5 如果需要项目源码的话,可以留下邮箱,先声明一下,该工具主要是为了实现自动同步输出代码类文件 ...

  9. 3D魔方游戏

    初学OpenGL时做的小程序,涉及到了OpenGL的大部分基本内容,如视图模型变换.色彩.纹理贴图.材质.光照.显示列表.选择等 三阶魔方有3×3×3个方块组成,每个方块的类当中都有一个4×4的矩阵, ...

  10. 【转载】高性能I/O设计模式Reactor和Proactor

    转载自:http://blog.csdn.net/roger_77/article/details/1555170 昨天购买了<程序员>杂志 2007.4期,第一时间去翻阅了一遍,其中有一 ...