您好,欢迎来到华佗小知识。
搜索
您的当前位置:首页C#秒杀程序源码

C#秒杀程序源码

来源:华佗小知识
C#通过socket实现淘宝秒杀器源码 - -

这个秒杀器的界面

先输入淘宝的账户和密码,然后贴上宝贝的地址 点击检测并抢购。 源码:

1 using System;

2 using System.Collections; 3 using System.Configuration; 4 using System.Data; 5 using System.Web;

6 using System.Web.Security; 7 using System.Web.UI;

8 using System.Web.UI.HtmlControls; 9 using System.Web.UI.WebControls; 10 using System.Net;

11 using System.Net.Sockets; 12 using System.Text;

13 using System.Threading; 14 using System.IO;

15 using System.Text.RegularExpressions; 16

17 public partial class MiaoSha : System.Web.UI.Page 18 {

19 string strServer = string.Empty; 20 string strPath = string.Empty; 21

22 protected void Page_Load(object sender, EventArgs e) 23 { 24 25 } 26

27 public static String Recv(Socket sock, Encoding encode) 28 {

29 Byte[] buffer = new Byte[8192];

30 StringBuilder sb = new StringBuilder();

31

32 Thread.Sleep(50);//根据页面响应时间进行微调 33 Int32 len = sock.Receive(buffer);

34 sb.Append(encode.GetString(buffer, 0, len)); 35

36 while (sock.Available > 0) 37 {

38 Thread.Sleep(300);//也可以视情况微调 39 Array.Clear(buffer, 0, buffer.Length); 40 len = sock.Receive(buffer);

41 sb.Append(encode.GetString(buffer, 0, len)); 42 string ss = encode.GetString(buffer, 0, len); 43 }

44 sock.Close();

45 return sb.ToString(); 46 } 47

48 ///

49 /// Socket获取页面HTML同时返回头信息 50 ///

51 /// 服务器地址或主机名 52 /// 请求的页面 53 /// post or get 54 /// 提交的数据 55 /// Cookies 56 /// 返回页面的HTML

57 public string GetHtml(string server, string url, string method, string data, string Cookies) 58 {

59 string _method = method.ToUpper(); 60 string _url = string.Empty; 61 if (url == \"\") 62 {

63 _url = \"/\"; }

65 else if (url.Substring(0, 1) != \"/\") 66 {

67 _url = \"/\" url; 68 } 69 else 70 {

71 _url = url; 72 }

73 string formatString = string.Empty; 74 string sendString = string.Empty;

75 Encoding ASCII = Encoding.Default; 76

77 //以下是拼接的HTTP头信息 78 if (_method == \"GET\") 79 {

80 formatString = \"\";

81 formatString = \"{0} {1} HTTP/1.1\\r\\n\"; 82 formatString = \"Host: {2}\\r\\n\";

83 formatString = \"User-Agent:Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7\\r\\n\";

84 formatString = \"Accept: text/html\\r\\n\"; 85 formatString = \"Keep-Alive: 300\\r\\n\"; 86 formatString = \"Cookies:{3}\\r\\n\";

87 formatString = \"Connection: keep-alive\\r\\n\\r\\n\";

88 sendString = string.Format(formatString, _method, _url, server, Cookies); } 90 else 91 {

92 formatString = \"\";

93 formatString = \"{0} {1} HTTP/1.1\\r\\n\"; 94 formatString = \"Host: {2}\\r\\n\";

95 formatString = \"User-Agent:Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7\\r\\n\";

96 formatString = \"Accept:text/html\\r\\n\";

97 formatString = \"Content-Type:application/x-www-form-urlencoded\\r\\n\"; 98 formatString = \"Content-Length:{3}\\r\\n\";

99 formatString = \"Referer:http://buy.taobao.com/auction/buy_now.jhtml\"; 100 formatString = \"Keep-Alive:300\\r\\n\"; 101 formatString = \"Cookies:{4}\\r\\n\";

102 formatString = \"Connection: keep-alive\\r\\n\\r\\n\"; 103 formatString = \"{5}\\r\\n\";

104 sendString = string.Format(formatString, _method, _url, server, Encoding.Default.GetByteCount(data), Cookies, data); 105 } 106

107 Byte[] ByteGet = ASCII.GetBytes(sendString); 108 Byte[] RecvBytes = new Byte[1024]; 109 String strRetPage = null;

110 IPAddress hostadd = Dns.Resolve(server).AddressList[0]; 111 IPEndPoint EPhost = new IPEndPoint(hostadd, 80);

112 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

113 s.Connect(EPhost); 114 if (!s.Connected)

115 {

116 strRetPage = \"链接主机失败\"; 117 return strRetPage; 118 }

119 s.Send(ByteGet, ByteGet.Length, SocketFlags.None); 120

121 strRetPage = Recv(s, ASCII); 122

123 return strRetPage; 124 } 125

126 protected void btnLogin_Click(object sender, EventArgs e) 127 {

128 string u = this.txtUserName.Text.Trim(); 129 string p = this.txtPwd.Text.Trim(); 130 DateTime st = DateTime.Now; 131

132 //淘宝登录需要post的数据串

133 string sendData = \"TPL_username=\" u \"&TPL_password=\" Server.UrlEncode(p)

\"&actionForStable=enable_post_user_action&action=Authenticator&mi_uid=&mcheck=&TPL_redirect_url=http%3A%2F%2Fitem.taobao.com%2Fauction%2Fitem_detail-0db1-3036113cf5455bd74047f1a581ba4be7.htm&_oooo_=http%3A%2F%2Fitem.taobao.com%2Fauction%2Fitem_detail-0db1-3036113cf5455bd74047f1a581ba4be7.htm&event_submit_do_login=anything&abtest=&pstrong=3&from=&yparam=&done=&loginType=3&tid=&support=000001&CtrlVersion=1%2C0%2C0%2C7\"; 134

135 string s = GetHtml(\"login.taobao.com\\"/member/login.jhtml\\"post\sendData, \"\");

136 Session[\"Cookies\"] = GetCookies(s); //从返回的源码中提取cookies,抓取登录后的页面需要附上该cookies 137

138 }

139 protected void btnBuy_Click(object sender, EventArgs e) 140 {

141 string strURL = this.txtURL.Text.Trim(); 142 getServerAndPath(strURL); 143

144 string s = GetHtml(strServer, strPath, \"get\145 //Response.Write(s);

146 if (s.IndexOf(\"立即购买\") > 0) 147 {

148 string item_id = strURL.Split('-')[2].Split('.')[0].ToString(); 149 string x_id = strURL.Split('-')[1].ToString();

150

151 s = GetHtml(\"buy.taobao.com\\"/auction/buy.htm?from=itemDetail&item_id=\" item_id \"&x_id=\" x_id, \"get\\"\Session[\"Cookies\"].ToString());

152 //Response.Write(s);

153 using (StreamWriter sw = new StreamWriter(Server.MapPath(\"debug1.html\"))) 154 {

155 sw.Write(s); 156 } 157

158 if (s.IndexOf(\"确认提交订单\") > 0) 159 {

160 Session[\"Cookies\"] = GetCookies(s); 161 string postData = getPostData(s);

162 string r = GetHtml(\"buy.taobao.com\\"/auction/buy_now.htm\\"post\postData, Session[\"Cookies\"].ToString());

163 if (r.IndexOf(\"302\") > 0) 1 {

165 using (StreamWriter sw = new StreamWriter(Server.MapPath(\"debug2.html\"))) 166 {

167 sw.Write(r); 168 } 169 } 170 else 171 {

172 //// 173 }

174 using (StreamWriter sw = new StreamWriter(Server.MapPath(\"debug2.html\"))) 175 {

176 sw.Write(r); 177 } 178 } 179 }

180 else if (s.IndexOf(\"btn-wait\") > 0)//该宝贝还处于定时上架的状态 181 { 182 183 } 184

185 } 186 187

188 ///

1 /// 从返回的源代码中提取cookies 190 ///

191 /// 192 ///

193 private string GetCookies(string s) 194 {

195 StringBuilder sbCookies = new StringBuilder(); 196

197 string[] arr = s.Split(new string[] StringSplitOptions.RemoveEmptyEntries); 198 foreach (string str in arr) 199 {

200 if (str.StartsWith(\"Set-Cookie: \")) 201 {

202 int intStart = str.IndexOf(\";\");

203 string strCookie = str.Substring(12, intStart - 11); 204 sbCookies.Append(strCookie); 205 } 206 }

207 return sbCookies.ToString(); 208 } 209

210 private string GetLocationURL(string s) 211 { 212

213 string RtnString = string.Empty;

214 StringBuilder sbCookies = new StringBuilder(); 215

216 string[] arr = s.Split(new string[] StringSplitOptions.RemoveEmptyEntries); 217 foreach (string str in arr) 218 {

219 if (str.StartsWith(\"Location: \")) 220 {

221 RtnString = str.Substring(11, str.Length - 11); 222 } 223 }

224 return RtnString; 225 } 226 227 228

229 private void getServerAndPath(string strURL)

{ \"\\r\\n\" { \"\\r\\n\" },

},

230 {

231 if (strURL != \"\" && strURL.IndexOf(\"/\") > 0) 232 {

233 int SlashPos = strURL.Substring(7).IndexOf(\"/\"); 234 strServer = strURL.Substring(7, SlashPos); 235 strPath = strURL.Substring(SlashPos 7); 236 } 237 else

238 return; 239 } 240 241 242

243 ///

244 /// 从最后确认购买页面的源代码中提取表单数据的数据 245 ///

246 /// 247 ///

248 private string getPostData(string html) 249 {

250 string postStr = \"\";

251 string pat = \"\"; 252 Regex regex = new Regex(pat, RegexOptions.Multiline | RegexOptions.IgnoreCase);

253 MatchCollection mcollection = regex.Matches(html); 254

255 foreach (Match m in mcollection) 256 {

257 GroupCollection gcollection = m.Groups;

258 if (m.ToString().IndexOf(\"_fma.b._0.s\") > 0) { continue; } 259 if (m.ToString().IndexOf(\"_fma.b._0.c\") > 0) { continue; }

260 if (m.ToString().IndexOf(\"isCheckCode\") > 0 && gcollection[2].Value.ToLower() == \"true\") 261 {

262 //isCheckCode = true; 263 }

2 postStr = gcollection[1].Value; postStr = \"=\"; 265 postStr = Server.UrlEncode(gcollection[2].Value); 266 postStr = \"&\"; 267 } 268 postStr = \"n_prov=370000&n_city=370500&n_area=370522&_fma.b._0.w=quicky&_fma.b._0.ac=250&consignment=10&_fma.b._0.au=5&_fma.b._0.c=8888\";

269 postStr = postStr.Replace(\"quantity=0\\"quantity=1\").Replace(\"_fma.b._0.d=您不

必重复省-市-区信息;至少5个字\\"_fma.b._0.d=\" Server.UrlEncode(\"收货人的具体地址\")).Replace(\"_fma.b._0.po=\\"_fma.b._0.po=230031\").Replace(\"_fma.b._0.de=\\"_fma.b._0.de=\" Server.UrlEncode(\"啊峰\")).Replace(\"_fma.b._0.u=\\"_fma.b._0.u=0\").Replace(\"_fma.b._0.di=1\\"_fma.b._0.di=370522\").Replace(\"_fma.b._0.deli=\\"_fma.b._0.deli=13888888888\");

270 postStr = \"&_fma.b._0.s=2\";

271 //postStr = Server.UrlEncode(postStr); 272

273 return postStr; 274 } 275 }

文章来自学IT网:http://www.xueit.com/html/2010-04/21-9446381762010415959440.html

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo0.cn 版权所有 湘ICP备2023017654号-2

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务