模拟京东商城登陆HttpRequest

2023-05-16

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

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

 


  1 using System;
  2 using System.Collections.Generic;
  3 using System.IO;
  4 using System.Linq;
  5 using System.Net;
  6 using System.Text;
  7 
  8 namespace HelperLib
  9 {
 10     public enum ResponeType
 11     {
 12         String,
 13         File
 14     }
 15     /// <summary>
 16     /// HttpRequest Help
 17     /// Code By:lvxiaojia
 18     /// blog:http://www.cnblogs.com/lvxiaojia/
 19     /// </summary>
 20     public class RequestHelp
 21     {
 22         static CookieContainer cookie = new CookieContainer();
 23         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)
 24         {
 25             var result = "";
 26             //var cookie = new CookieContainer();
 27             StringBuilder strPostData = new StringBuilder();
 28             if (postData != null)
 29             {
 30                 postData.AsQueryable().ToList().ForEach(a =>
 31                 {
 32                     strPostData.AppendFormat("{0}={1}&", a.Key, a.Value);
 33                 });
 34             }            
 35             byte[] byteArray = Encoding.UTF8.GetBytes(strPostData.ToString().TrimEnd('&'));
 36 
 37             HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
 38 
 39             webRequest.CookieContainer = cookie;
 40 
 41             webRequest.Method = "POST";
 42             if (string.IsNullOrEmpty(accept))
 43                 webRequest.Accept = "application/json, text/javascript, */*;";
 44             else
 45                 webRequest.Accept = accept;
 46 
 47             if (!string.IsNullOrEmpty(referer))
 48                 webRequest.Referer = referer;
 49             if (string.IsNullOrEmpty(contentType))
 50                 webRequest.ContentType = "application/x-www-form-urlencoded";
 51             else
 52                 webRequest.ContentType = contentType;
 53 
 54             if (strPostData.Length > 0)
 55                 webRequest.ContentLength = byteArray.Length;
 56 
 57             //请求
 58             Stream newStream = webRequest.GetRequestStream();
 59             newStream.Write(byteArray, 0, byteArray.Length);
 60             newStream.Close();
 61 
 62             HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
 63             var responSteam = response.GetResponseStream();
 64 
 65             if (type == ResponeType.String)
 66             {
 67                 StreamReader strRespon = new StreamReader(responSteam, Encoding.UTF8);
 68                 result = strRespon.ReadToEnd();
 69             }
 70             else
 71             {
 72                 BinaryReader br = new BinaryReader(responSteam);
 73                 byte[] byteArr = br.ReadBytes(200000);
 74                 FileStream fs = new FileStream(fileSavePath, FileMode.OpenOrCreate);
 75                 fs.Write(byteArr, 0, byteArr.Length);
 76                 fs.Dispose();
 77                 fs.Close();
 78                 result = "OK";
 79             }
 80             if (action != null)
 81             {
 82                 action.Invoke(result);
 83             }
 84             if (fun != null)
 85             {
 86                 Dictionary<string, string> dic = new Dictionary<string, string>();
 87                 foreach (var item in cookie.GetCookies(webRequest.RequestUri))
 88                 {
 89                     var c = item as Cookie;
 90                     dic.Add(c.Name, c.Value);
 91                 }
 92                 fun = () => { return dic; };
 93             }
 94             return result;
 95 
 96         }
 97 
 98 
 99         public static string Get(string url, Dictionary<string, string> postData=null, string referer = "", Action<string> action = null, Action<Dictionary<string, string>> fun = null)
100         {
101             var result = "";
102             
103             StringBuilder strPostData = new StringBuilder("?");
104             if (postData != null)
105             {
106                 postData.AsQueryable().ToList().ForEach(a =>
107                 {
108                     strPostData.AppendFormat("{0}={1}&", a.Key, a.Value);
109                 });
110             }
111             if (strPostData.Length == 1)
112                 strPostData = strPostData.Clear();
113             HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url + strPostData.ToString().TrimEnd('&'));
114             webRequest.CookieContainer = cookie;
115             webRequest.Method = "GET";
116             webRequest.Accept = "text/javascript, text/html, application/xml, text/xml, */*;";
117             if (!string.IsNullOrEmpty(referer))
118                 webRequest.Referer = referer;
119             //请求
120             HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
121             var responSteam = response.GetResponseStream();
122 
123             StreamReader strRespon = new StreamReader(responSteam, Encoding.Default);
124             result = strRespon.ReadToEnd();
125 
126             if (action != null)
127             {
128                 action.Invoke(result);
129             }
130             if (fun != null)
131             {
132                 Dictionary<string, string> dic = new Dictionary<string, string>();
133                 foreach (var item in cookie.GetCookies(webRequest.RequestUri))
134                 {
135                     var c = item as Cookie;
136                     dic.Add(c.Name, c.Value);
137                 }
138                 fun.Invoke(dic);
139             }
140             return result;
141 
142         }
143 
144     }
145 }  
RequestHelp

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Linq;
 5 using System.Reflection;
 6 using System.Security.Cryptography;
 7 using System.Text;
 8 
 9 namespace HelperLib
10 {
11     /// <summary>
12     /// 
13     /// </summary>
14     public class EncodingHelp
15     {
16         public static string GetMd5Str32(string str)
17         {
18             MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
19             char[] temp = str.ToCharArray();
20             byte[] buf = new byte[temp.Length];
21             for (int i = 0; i < temp.Length; i++)
22             {
23                 buf[i] = (byte)temp[i];
24             }
25             byte[] data = md5Hasher.ComputeHash(buf);
26             StringBuilder sBuilder = new StringBuilder();
27             for (int i = 0; i < data.Length; i++)
28             {
29                 sBuilder.Append(data[i].ToString("x2"));
30             }
31             return sBuilder.ToString();
32         }
33 
34         public static string GetEnumDescription(Enum value)
35         {
36             Type enumType = value.GetType();
37             string name = Enum.GetName(enumType, value);
38             if (name != null)
39             {
40                 // 获取枚举字段。
41                 FieldInfo fieldInfo = enumType.GetField(name);
42                 if (fieldInfo != null)
43                 {
44                     // 获取描述的属性。
45                     DescriptionAttribute attr = Attribute.GetCustomAttribute(fieldInfo,
46                         typeof(DescriptionAttribute), false) as DescriptionAttribute;
47                     if (attr != null)
48                     {
49                         return attr.Description;
50                     }
51                 }
52             }
53             return null;
54         }
55     }
56 }  
EncodingHelp

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Diagnostics;
  6 using System.Drawing;
  7 using System.IO;
  8 using System.Linq;
  9 using System.Net;
 10 using System.Text;
 11 using System.Text.RegularExpressions;
 12 using System.Web;
 13 using System.Windows.Forms;
 14 using HelperLib;
 15 
 16 namespace SimulationSouGouLogion
 17 {
 18     public partial class LoginJD : Form
 19     {
 20         public LoginJD()
 21         {
 22             InitializeComponent();
 23         }
 24 
 25         static string loginUrl = "https://passport.jd.com/new/login.aspx";
 26         static string loginServiceUrl = "http://passport.jd.com/uc/loginService";
 27         static string loginRefererUrl = "http://passport.jd.com/uc/login?ltype=logout";
 28 
 29         private void button1_Click(object sender, EventArgs e)
 30         {
 31             richTextBox1.Text = "";
 32 
 33             RequestHelp.Get(loginUrl, null);
 34 
 35             var login = RequestHelp.Post(loginServiceUrl,
 36                 new Dictionary<string, string>(){
 37                 {"uuid","59c439c8-09de-4cce-8293-7a296b0c0dd1"},
 38                 {"loginname",HttpUtility.UrlEncode(tbUserCode.Text)},
 39                 {"nloginpwd",HttpUtility.UrlEncode(tbPassword.Text)},
 40                 {"loginpwd",HttpUtility.UrlEncode(tbPassword.Text)},
 41                 {"machineNet","machineCpu"},
 42                 {"machineDisk",""},
 43                 {"authcode",""}}, loginRefererUrl);
 44 
 45             if (!login.Contains("success"))
 46             {
 47                 if (login.ToLower().Contains("pwd"))
 48                 {
 49                     MessageBox.Show("密码验证不通过!", "提示");
 50                     tbPassword.Text = "";
 51                     tbPassword.Focus();
 52                     return;
 53                 }
 54                 else
 55                 {
 56                     MessageBox.Show("登陆失败!", "提示"); return;
 57                 }
 58             }
 59 
 60 
 61             var dic = new Dictionary<string, string>();
 62             //获取订单列表
 63             var orderList = RequestHelp.Get("http://order.jd.com/center/list.action?r=635133982534597500", null, fun: a => dic = a);
 64 
 65             //分析网页HTML
 66             var regexOrder = Repex(@"<tr id=.{1}track\d+.{1} oty=.{1}\d{1,3}.{1}>.*?</tr>?", orderList);
 67 
 68             //获取每个订单信息
 69             regexOrder.ForEach(a =>
 70             {
 71                 var orderCode = Repex(@"<a name=.{1}orderIdLinks.{1} .*? href=.{1}(.*)'?.{1}>(.*)?</a>?", a);
 72                 var order = Match(@"<a name='orderIdLinks' .*? href='(.*)?' clstag=.*>(.*)</a>", orderCode[0]);
 73                 richTextBox1.Text += "订单号:" + order[1] + "\r\n";
 74                 richTextBox1.Text += "订单详情地址:" + order[0] + "\r\n";
 75             });
 76 
 77             //获取订单信息
 78             //var details = RequestHelp.Get("http://jd2008.jd.com/jdhome/CancelOrderInfo.aspx?orderid=502561335&PassKey=28C8E5A477E7B255A72A7A67841D5D13");
 79         }
 80 
 81         List<string> Repex(string parm, string str)
 82         {
 83             List<string> list = new List<string>();
 84             var regex = new Regex(parm, RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.IgnoreCase);
 85             var math = regex.Matches(str);
 86             if (math.Count < 0) return list;
 87 
 88             foreach (var item in math)
 89             {
 90                 var strdetails = (item as Match).Value;
 91 
 92                 list.Add(strdetails);
 93             }
 94 
 95             return list;
 96 
 97         }
 98         List<string> Match(string parm, string str)
 99         {
100             List<string> list = new List<string>();
101             var math = Regex.Matches(str, parm);
102             if (math.Count < 0) return list;
103             list.Add(math[0].Result("$1"));
104             list.Add(math[0].Result("$2"));
105             return list;
106 
107         }
108 
109         private void Form1_Load(object sender, EventArgs e)
110         {
111             tbPassword.Focus();
112         }
113 
114         private void blogLink_Click(object sender, EventArgs e)
115         {
116             Process.Start("http://www.cnblogs.com/lvxiaojia/");
117         }
118 
119     }
120 }  
窗体后台代码

 

转载于:https://www.cnblogs.com/lvxiaojia/p/3292689.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐

  • 解决mac安装homebrew后报错-bash: brew: command not found

    参照官网上很简单的一句安装命令 xff0c usr bin ruby e 34 curl fsSL https raw githubusercontent com Homebrew install master install 34 安装完
  • 服务器cpu虚拟化怎么开启,开启cpu虚拟化

    开启cpu虚拟化 内容精选 换一换 VirtualBox是一款开源免费跨平台的虚拟机软件 xff0c 本节指导用户完成VirtualBox的安装 安装VirtualBox的主机需满足以下条件 推荐使用64位的Windows操作系统的主机安装
  • 注册表修改3389端口号

    用注册表修改3389端口号 1 改端口 xff1a 简单操作步骤 xff1a 打开 34 开始 运行 34 xff0c 输入 34 regedit 34 xff0c 打开注册表 xff0c 进入以下路径 xff1a HKEY LOCAL M
  • 这个项目碉堡了

    新年第一天上班 xff0c 没想到就立春了 xff0c 俗话说 xff0c 一年之计在于春 xff0c 全新的 17 年开始啦 xff0c 来 xff0c 收拾下心情 xff0c 投入到工作中 xff0c 撸起袖子 xff0c 就是干 xf
  • RedHat 6.5(x86_64)启动nagios客户端nrpe报错的解决方法

    tar xvf nagios tar gz C usr local usr local nagios bin nrpe c usr local nagios etc nrpe cfg d bash usr local nagios bin
  • wifi dhcp linux,archlinux 安装前的网络设置 静态IP DHCP 无线WIFI

    安装版本archlinux 20200701 xff0c 在安装前的网络配置 一 准备阶段 xff0c 查看网卡状态是否up xff0c 设置网卡为up状态 查看网卡信息 ip link 如果要使用的网卡包含state down字段 xff
  • java 判断 string null_java 字符串为null 如何判断

    判别一个字符串str不为空的办法有 xff1a 1 str 61 61 null 2 str isEmpty str 61 61 null 是有必要存在的 假如 String 类型为null 而去停止 equals String 或 len
  • What is my IP?

    今天介绍2个小工具 放心 xff0c 都是绿色的 xff0c 而且免安装 第一个叫whatismyip com 正如其名 xff0c 这个工具是用来看自己的IP的 啥 xff1f 你觉得太胡扯 xff1f 你是不是觉得看自己IP地址太简单
  • libqxt编译

    一 说明 编译环境 xff1a win10 qt5 6 1 1 vs2013和libqxt源码 从git上下载 libqxt xff1a libqxt 关于libqxt的说明 xff0c 请到libqxt的官网阅读 xff0c 说着看图1
  • ASP.NET CORE系列【五】webapi整理以及RESTful风格化

    原文 ASP NET CORE系列 五 webapi整理以及RESTful风格化 介绍 什么是RESTful xff1f 这里不多做赘述 xff0c 详情请百度 xff01 哈哈 xff0c 本来还想巴拉巴拉介绍一些webapi RESTf
  • mac系统如何生成SSH key与GitHub通信

    一 检查 SSH key 是否存在 在终端输入 xff1a ls al ssh 如果没有 xff0c 终端显示如下 xff1a No such file or directory 如果已经存在 xff0c 则会显示 id rsa 和 id
  • TortoiseSVN 忽略文件 忽略已提交文件

    主要以下两种情况 xff1a 1 首次提交就做好了忽略拦截 xff1a 项目首次提交到svn服务器的时候 xff0c 把该删的删了 xff0c 然后设置忽略规则 xff0c 就没问题了 2 提交一段时间忽然想忽略拦截 xff1a 经常碰到的
  • java里getter和setter的作用和区别是什么?

    java是典型的面向对象的编程语言 xff0c 面向对象三个特性 xff0c 继承性 xff0c 多态性 xff0c 封装性 xff0c 主要和封装性考虑 xff0c 类里面的变量不想设置成公共的类型 xff0c 但是还要给外部使用在这种实
  • FC金手指使用方法+大全

    一 文章来由 童年 小时候除了小霸王FC主机 xff0c 然后就是世嘉MD主机 xff0c 玩的好多啊 xff0c 但有些游戏一直没打穿留下遗憾 网上找金手指使用方法 xff0c 都真真假假 xff0c 鱼龙混杂 xff0c 试了很多终于得
  • shell根据关键字获取文件某一行的行号

    为什么80 的码农都做不了架构师 xff1f gt gt gt cat n 文件名 grep 39 关键字 39 awk 39 print 1 39 cat n是获取行号 xff0c 要是获取行内容 xff0c 去掉 n就可以了 转载于 h
  • VS Code编译支持C++11问题

    2019独角兽企业重金招聘Python工程师标准 gt gt gt 如果你正确配置了 xff0c 能正确编译c 43 43 xff0c 但是发现auto等一些关键词不能使用 xff0c 那么 xff0c 请尝试如下操作 xff1a 打开ta
  • word2007自动生成参考文献引用并且右上角标注

    2019独角兽企业重金招聘Python工程师标准 gt gt gt 在写毕业论文时 xff0c 总要处理四五十篇的参考文献的引用 xff0c 本文就介绍如何快捷自动生成参考文献引用 xff0c 同时实现参考文献右上角标注 打开需要排版的论文
  • matlab练习程序(随机粒子切换特效)

    视频制作软件中一般都会有相邻帧切换的特效 xff0c 我过去用过vagas好像就有很多切换特效 我想这个也算是其中一种吧 xff0c 虽然我不确定实际中到底有没有这种切换 实际上我只是下班后太无聊了 xff0c 写着玩的 xff0c 没什么
  • PyQt4(简单信号槽)

    import sys from PyQt4 import QtCore QtGui class myWidget QtGui QWidget def init self super myWidget self init self setWi
  • 模拟京东商城登陆HttpRequest

    利用Winform HttpRequest 模拟登陆京东商城 目前只获取订单信息 xff0c 可以获取图片等其他信息 1 using System 2 using System Collections Generic 3 using Sys