/// <summary> /// Submit Order to http://shop.48.cn/TOrder/tickCheck /// Result in JSON with Order ID /// ReturnObject=/TOrder/Item/161116-2-100207 /// </summary> /// <returns>void</returns> public string CheckOrder(int showID, int seatType) { if (showID <= 0) { // Need handle invalid show id and seat type here. // Current no idea on how to handle it. } string orderSNHSysName = string.Empty; try { HttpWebRequest request = ComposeCheckOrderRequestHeader(ps_shop48cn_orderRequestType_GET); HttpWebResponse response = response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { string stringContainer = new StreamReader(response.GetResponseStream()).ReadToEnd().Trim(); JavaScriptSerializer jsSerializer = new JavaScriptSerializer(); Hashtable resDict = (Hashtable)jsSerializer.Deserialize(stringContainer, typeof(Hashtable)); SubmitCheckOrderResponse CheckOrderResponse = new SubmitCheckOrderResponse(bool.Parse(resDict["HasError"].ToString()), resDict["ErrorCode"], resDict["Message"], resDict["ReturnObject"]); // ErrorCode=wait, HasError=False, Message=null, ReturnObject=null if (CheckOrderResponse.ErrorCode.ToString() == "wait" && !CheckOrderResponse.HasError) { orderSNHSysName = "wait"; } // ErrorCode=success, HasError=False, Message=ok, ReturnObject=/TOrder/Item/161123-2-101262 else if (!CheckOrderResponse.HasError && (CheckOrderResponse.ErrorCode != null && CheckOrderResponse.ErrorCode.ToString() == "success") && CheckOrderResponse.ReturnObject != null) { orderSNHSysName = CheckOrderResponse.ReturnObject.ToString(); } // retry else { orderSNHSysName = string.Empty; } } } catch (Exception ex) { } return(orderSNHSysName); }
/// <summary> /// Submit Order to http://shop.48.cn/TOrder/add /// Result in JSON with Order ID /// ReturnObject=/TOrder/Item/161116-2-100207 /// </summary> /// <returns>void</returns> public bool SubmitOrder() { int showID = this.p_order.ShowID; int seatType = this.p_order.SeatType; int ticketCount = this.p_order.TicketNO; Stream requestStream = null; Random rnd = new Random(); bool result = false; string orderSNHSysName = string.Empty; // Compose Post Data string postDataFormat = "id={0}&num={1}&seattype={2}&brand_id=2&r={3}"; string postData = string.Format(postDataFormat, showID, ticketCount, seatType, rnd.NextDouble().ToString()); byte[] postByteData = Encoding.UTF8.GetBytes(postData); // wait to be notified by main thread. OrderHandler._StartTimeManualResetEvent.WaitOne(); try { string stringContainer; JavaScriptSerializer jsSerializer; Hashtable resDict; SubmitCheckOrderResponse submitOrderResponse = null; HttpWebRequest request = ComposeSubmitOrderRequestHeader(ps_shop48cn_orderRequestType_POST); request.ContentLength = postByteData.Length; requestStream = request.GetRequestStream(); requestStream.Write(postByteData, 0, postByteData.Length); HttpWebResponse response = response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { // Get Response Body stringContainer = new StreamReader(response.GetResponseStream()).ReadToEnd().Trim(); jsSerializer = new JavaScriptSerializer(); resDict = (Hashtable)jsSerializer.Deserialize(stringContainer, typeof(Hashtable)); submitOrderResponse = new SubmitCheckOrderResponse(bool.Parse(resDict["HasError"].ToString()), resDict["ErrorCode"], resDict["Message"], resDict["ReturnObject"]); } // If respnse is null, resend request // If respnse.statuscode is not OK, resend reqeust // If earlier than 8:00pm, return "尚未开始" while (response.StatusCode != HttpStatusCode.OK || submitOrderResponse.Message == null || (submitOrderResponse.Message.ToString() != "success" && submitOrderResponse.Message.ToString() != "下单申请成功")) { requestStream.Close(); Thread.Sleep(2000); // Send Request Header & Body HttpWebRequest requestAgain = ComposeSubmitOrderRequestHeader(ps_shop48cn_orderRequestType_POST); requestAgain.ContentLength = postByteData.Length; requestStream = requestAgain.GetRequestStream(); requestStream.Write(postByteData, 0, postByteData.Length); response = (HttpWebResponse)requestAgain.GetResponse(); // Get body if (response.StatusCode == HttpStatusCode.OK) { stringContainer = new StreamReader(response.GetResponseStream()).ReadToEnd().Trim(); jsSerializer = new JavaScriptSerializer(); resDict = (Hashtable)jsSerializer.Deserialize(stringContainer, typeof(Hashtable)); submitOrderResponse = new SubmitCheckOrderResponse(bool.Parse(resDict["HasError"].ToString()), resDict["ErrorCode"], resDict["Message"], resDict["ReturnObject"]); } } // not queued, get ticket immediately successfully // OrderResponse.Message = success && ReturnObject like /TOrder/Item/ if (submitOrderResponse.Message.ToString() == "success" && submitOrderResponse.ReturnObject.ToString().Contains("/TOrder/Item")) { result = true; orderSNHSysName = submitOrderResponse.ReturnObject.ToString(); } // Need Queue. Need Check Ticket continuously per 5 seconds. if (submitOrderResponse.Message.ToString() == "下单申请成功" && submitOrderResponse.ReturnObject == null) { Thread.Sleep(1000); while (orderSNHSysName == "wait" || orderSNHSysName == string.Empty) { orderSNHSysName = CheckOrder(showID, seatType); Thread.Sleep(5000); } } } catch (Exception ex) { } finally { requestStream.Close(); } return(result); }