public static string getSiteData(String urlReq, int timeout) { Stopwatch sw = Stopwatch.StartNew(); try { //GrabAgent.trace_log("+++++++++++++++++++++ {{"); HttpWebRequest request = (HttpWebRequest) WebRequest.Create(urlReq); request.Headers.Add("X-Fsign", "SW9D1eZo"); request.Timeout = timeout; //5 seconds HttpWebResponse response = (HttpWebResponse) request.GetResponse(); // we will read data via the response stream Stream resStream = response.GetResponseStream(); resStream.ReadTimeout = timeout; StringBuilder sb = new StringBuilder(); byte[] buf = new byte[8192]; string tempString = null; int count = 0; do { // fill the buffer with data count = resStream.Read(buf, 0, buf.Length); // make sure we read some data if (count != 0) { // translate from bytes to ASCII text tempString = Encoding.UTF8.GetString(buf, 0, count); // continue building the string sb.Append(tempString); } } while (count > 0); // any more data to read? return(sb.ToString()); } catch (Exception ex) { //GrabAgent.trace_err("Http connection timeout(" + sw.ElapsedMilliseconds + ") at URL(" + urlReq + ")"); GrabAgent.trace_err("Http connection timeout(" + sw.ElapsedMilliseconds + ") at URL(" + "**" + ")"); return(null); } finally { sw.Stop(); //GrabAgent.trace_log("--------------------------------------------- " + sw.ElapsedMilliseconds + "}}"); } return(""); // null }
public override bool _updateData(string gameData) { if (gameData == "" || gameData == null) { GrabAgent.trace_log("No data."); return(false); } if (!openConnection()) { return(false); } String sqlRetrieve = "select * from game_log g where g_code=@c1 and g_type=@c2"; MySqlCommand cmdRetrieve = new MySqlCommand(sqlRetrieve, conn); cmdRetrieve.Prepare(); String sqlUpdate = "update game_log set s0=@s0, s1=@s1, s2=@s2, " + " a0=@a0, a1=@a1, a2=@a2, a3=@a3, a4=@a4, a5=@a5, a6=@a6, a7=@a7, a8=@a8, a9=@a9, " + " b0=@b0, b1=@b1, b2=@b2, b3=@b3, b4=@b4, b5=@b5, b6 =@b6, b7=@b7, b8=@b8, b9=@b9, " + " updated = unix_timestamp() " + " where 1=1 and g_code=@c1 and g_type=@c2"; MySqlCommand cmdUpdate = new MySqlCommand(sqlUpdate, conn); cmdUpdate.CommandTimeout = 2000; cmdUpdate.Prepare(); try { StringRecord aRecord = null; string sg_type = ""; String[] AAs = gameData.Split('~'); StringRecord zRecord = null; MySqlDataAdapter da = new MySqlDataAdapter(cmdRetrieve); DataTable dt = new DataTable(); DataRow dr; int typeCode = 0; foreach (string s in AAs) { aRecord = new StringRecord(s); if (s.StartsWith("SA")) { typeCode = int.Parse(aRecord.getField("SA")); sg_type = getGameType(typeCode); } else if (s.StartsWith("ZA")) { //no such case } else if (s.StartsWith("AA")) { if (sg_type == "") { continue; //unregistered game } try { //retrieve exisiting data mysqlSetParam(cmdRetrieve, "@c1", aRecord.getField("AA")); mysqlSetParam(cmdRetrieve, "@c2", typeCode); dt.Clear(); da.Fill(dt); dr = dt.Rows[0]; // update operation // mysqlSetParam(cmdUpdate, "@s0", aRecord.getField("AB") ?? dr["s0"]); //equivalent as bellow //status data mysqlSetParam(cmdUpdate, "@s0", gmv(aRecord, "AB", dr, "s0")); mysqlSetParam(cmdUpdate, "@s1", gmv(aRecord, "AC", dr, "s1")); mysqlSetParam(cmdUpdate, "@s2", gmv(aRecord, "AO", dr, "s2")); //teamA data mysqlSetParam(cmdUpdate, "@a0", gmv(aRecord, "AG", dr, "a0")); mysqlSetParam(cmdUpdate, "@a1", gmv(aRecord, "BA", dr, "a1")); mysqlSetParam(cmdUpdate, "@a2", gmv(aRecord, "BC", dr, "a2")); mysqlSetParam(cmdUpdate, "@a3", gmv(aRecord, "BE", dr, "a3")); mysqlSetParam(cmdUpdate, "@a4", gmv(aRecord, "BG", dr, "a4")); mysqlSetParam(cmdUpdate, "@a5", gmv(aRecord, "BI", dr, "a5")); mysqlSetParam(cmdUpdate, "@a6", gmv(aRecord, "DA", dr, "a6")); mysqlSetParam(cmdUpdate, "@a7", gmv(aRecord, "DC", dr, "a7")); mysqlSetParam(cmdUpdate, "@a8", gmv(aRecord, "DE", dr, "a8")); mysqlSetParam(cmdUpdate, "@a9", gmv(aRecord, "AT", dr, "a9")); //teamB data mysqlSetParam(cmdUpdate, "@b0", gmv(aRecord, "AH", dr, "b0")); mysqlSetParam(cmdUpdate, "@b1", gmv(aRecord, "BB", dr, "b1")); mysqlSetParam(cmdUpdate, "@b2", gmv(aRecord, "BD", dr, "b2")); mysqlSetParam(cmdUpdate, "@b3", gmv(aRecord, "BF", dr, "b3")); mysqlSetParam(cmdUpdate, "@b4", gmv(aRecord, "BH", dr, "b4")); mysqlSetParam(cmdUpdate, "@b5", gmv(aRecord, "BJ", dr, "b5")); mysqlSetParam(cmdUpdate, "@b6", gmv(aRecord, "DB", dr, "b6")); mysqlSetParam(cmdUpdate, "@b7", gmv(aRecord, "DD", dr, "b7")); mysqlSetParam(cmdUpdate, "@b8", gmv(aRecord, "DF", dr, "b8")); mysqlSetParam(cmdUpdate, "@b9", gmv(aRecord, "AU", dr, "b9")); //game code string ss = aRecord.getField("AA"); mysqlSetParam(cmdUpdate, "@c1", ss); mysqlSetParam(cmdUpdate, "@c2", typeCode); int rows = cmdUpdate.ExecuteNonQuery(); if (rows > 1) { //System.Windows.Forms.MessageBox.Show(rows + " rows updated. It's strange!!!!!!\r\n" + "G_CODE=" + ss + "G_TYPE=" + sg_type);//??? } } catch (IndexOutOfRangeException ie) { //String rd = SiteHelper.getSiteData(urlRetrieveAll); //_registerData(rd);??? } catch (Exception e) { GrabAgent.trace_err(e.Message); } } } GrabAgent.trace_log("Data updated."); } catch (Exception e) { GrabAgent.trace_log(e.Message); } return(true); }