public CookieCollection login(String phone, String pwd) { try { Encoding encoding = Encoding.GetEncoding("gb2312"); Random ra = new Random(); String getCodeURL = "http://sale.dooyo.cn//tuan/code.html?tradeId=getAdCode&t=0." + ra.Next(); HttpWebResponse responseCode = HttpWebResponseUtility.CreateGetHttpResponse(getCodeURL, null, null, null); if (responseCode.StatusCode == HttpStatusCode.OK) { System.IO.Stream resStream = responseCode.GetResponseStream(); System.Drawing.Image i = Image.FromStream(resStream); CheckCodeParser _parser = new CheckCodeParser(); String checkCode = _parser.parse(i); responseCode.Close(); IDictionary <string, string> loginParams = new Dictionary <string, string>(); loginParams.Add("autoLogin", "true"); loginParams.Add("tradeId", "webLogin"); loginParams.Add("user_id", phone); loginParams.Add("verify_code", checkCode); loginParams.Add("web_login_pwd", pwd); String set_cookie = responseCode.Headers["Set-Cookie"]; String[] sub = set_cookie.Split(new Char[] { ';' }); String[] cookies = sub[0].Split(new Char[] { '=' }); Cookie ck0 = new Cookie(cookies[0], cookies[1]); ck0.Domain = "sale.dooyo.cn"; CookieCollection cc0 = new CookieCollection(); cc0.Add(ck0); Cookie ck1 = new Cookie("activeBankName", "abc"); ck1.Domain = "sale.dooyo.cn"; cc0.Add(ck1); Cookie ck2 = new Cookie("path", "/tuan/miao/"); ck2.Domain = "sale.dooyo.cn"; cc0.Add(ck2); HttpWebResponse responseLogin = HttpWebResponseUtility.CreatePostHttpResponse( mLoginUrl, loginParams, null, null, encoding, cc0, false); if (responseLogin.StatusCode == HttpStatusCode.Found) { CookieCollection cc = responseLogin.Cookies; responseLogin.Close(); cc.Add(ck1); return(cc); } } return(new CookieCollection()); } catch (Exception ex) { String er = ex.ToString(); return(new CookieCollection()); } }
String miaoshaWork(UserManager.User u) { try { String msg = ""; Encoding encoding = Encoding.GetEncoding("gb2312"); CookieCollection cookieCollection = u._cookies; //获取验证码 Random ra = new Random(); String getCodeURL = "http://sale.dooyo.cn/tuan/code.html?tradeId=getMiaoShaCode&t=" + ra.Next(); HttpWebResponse response = HttpWebResponseUtility.CreateGetHttpResponse(getCodeURL, null, null, cookieCollection); if (response.StatusCode == HttpStatusCode.OK) { System.IO.Stream resStream = response.GetResponseStream(); System.Drawing.Image i = Image.FromStream(resStream); CheckCodeParser mParser = u._parser; String checkCode = mParser.parse(i); response.Close(); //log("获取验证码:" + checkCode); try { //验证验证码 // url:"/tuan/miao/miaoindex.html?tradeId=checkCodeForImmediately", String checkCodeURL = "http://sale.dooyo.cn/tuan/miao/miaoindex.html?tradeId=checkCode"; //checkCodeURL = "http://sale.dooyo.cn/tuan/miao/miaoindex.html?tradeId=checkCodeForImmediately"; IDictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("code", checkCode); parameters.Add("product_id", mProductID); HttpWebResponse response2 = HttpWebResponseUtility.CreatePostHttpResponse( checkCodeURL, parameters, null, null, encoding, cookieCollection, true); if (response2.StatusCode == HttpStatusCode.OK) { // log("验证验证码:" + checkCode); System.IO.StreamReader sr = new System.IO.StreamReader(response2.GetResponseStream()); String content = sr.ReadToEnd(); //这里的content就是网页内容了 sr.Close(); response2.Close(); //log("验证验证码:" + content); if (content.Contains("false")) { //验证失败 msg = "ER:验证码错误"; } else { msg = "验证成功"; //提交form String submitURL = "http://sale.dooyo.cn/tuan/miao/miaoConfirmOrder.html?tradeId=toMiaoOrder&product_id=" + mProductID; IDictionary <string, string> parameters2 = new Dictionary <string, string>(); parameters2.Add("code", checkCode); parameters2.Add("vCode", checkCode); parameters2.Add("userOrdersCount", "0"); parameters2.Add("maxOrdersCount", "2");//2 parameters2.Add("payTypeRadio", "ABC2"); //parameters2.Add("activityCode", "1");//2 //parameters2.Add("max_purchase_profit", "0.0");//2 //parameters2.Add("mobile", u._phone);//2 //parameters2.Add("order_sizecolor", "");//2 parameters2.Add("product_id", this.mProductID);//2 // parameters2.Add("product_props", "");//2 //parameters2.Add("quantity", "1");//2 // parameters2.Add("repeatOrderId", "");//2 // parameters2.Add("token", "1375190998404");//2 HttpWebResponse response3 = HttpWebResponseUtility.CreatePostHttpResponse( submitURL, parameters2, null, null, encoding, cookieCollection, true); if (response3.StatusCode == HttpStatusCode.OK) { //log("提交form"); System.IO.StreamReader sr3 = new System.IO.StreamReader(response3.GetResponseStream()); String content3 = sr3.ReadToEnd(); //这里的content就是网页内容了 sr3.Close(); response3.Close(); if (content3.Contains("抱歉")) { msg = "ER:提交表单失败啦"; } else { //下单 IDictionary <string, string> buyParams = new Dictionary <string, string>(); buyParams.Add("code", checkCode); buyParams.Add("vCode", checkCode); buyParams.Add("mobile", u._phone); //buyParams.Add("payTypeRadio", "1"); buyParams.Add("product_id", this.mProductID); buyParams.Add("product_props", ""); buyParams.Add("product_type", "0"); buyParams.Add("payTypeRadio", "ABC2"); buyParams.Add("quantity", "1"); buyParams.Add("userOrdersCount", "1"); buyParams.Add("maxOrdersCount", "10"); HttpWebResponse responseBuy = HttpWebResponseUtility.CreatePostHttpResponse( mBuyURL, buyParams, null, null, encoding, cookieCollection, true); if (responseBuy.StatusCode == HttpStatusCode.OK) { //log("下单"); System.IO.StreamReader srBuy = new System.IO.StreamReader(responseBuy.GetResponseStream()); String contentBuy = srBuy.ReadToEnd(); //这里的content就是网页内容了 srBuy.Close(); //成功啦 if (contentBuy.Contains("抱歉") || contentBuy.Contains("未开始秒杀")) { msg = "ER:下单失败啦"; } else { msg = "DONE:快付款吧"; } } else { //log("下单错误,返回code:" + responseBuy.StatusCode); } } //if(!content3.Contains("抱歉")) } //if (response3.StatusCode == HttpStatusCode.OK) else { //log("提交form错误,返回code:" + response3.StatusCode); } } } } catch (Exception ex) { String str = ex.ToString(); str = "ER:" + str; } } mStr = msg; return(msg); } catch (Exception ex) { String str = "ER:出错了" + ex.ToString(); return(str); } }