public bool readAndSaveWagonToDB(long wagonNo, string solarDateFrom, string solarDateTo, int cycleNumber) { SharedVariables.logs.Info("Start Fetch " + wagonNo + " from " + solarDateFrom + " to " + solarDateTo); using (var entityLogistics = new logisticEntities()) { customersHistoryFetchLog entryCustomersHistoryFetchLog = new customersHistoryFetchLog(); entryCustomersHistoryFetchLog.endDate = solarDateTo; entryCustomersHistoryFetchLog.fetchTimeEnd = null; entryCustomersHistoryFetchLog.fetchTimeStart = DateTime.Now; entryCustomersHistoryFetchLog.startDate = solarDateFrom; entryCustomersHistoryFetchLog.state = 0; entryCustomersHistoryFetchLog.wagonNo = wagonNo.ToString(); entityLogistics.customersHistoryFetchLogs.Add(entryCustomersHistoryFetchLog); entityLogistics.SaveChanges(); wagonInfoTrackingDetail wagonDetail = new wagonInfoTrackingDetail(); try { DateTime dateFrom, dateTo; dateFrom = DateTime.MinValue; dateTo = DateTime.MaxValue; try { dateFrom = Functions.solarToMiladi(solarDateFrom); } catch (Exception ex) { SharedVariables.logs.Error("wagonInfoSeirHistory.readAndSaveWagonToDB.solarToMiladi,solarDateFrom", ex); entryCustomersHistoryFetchLog.state = -1; entryCustomersHistoryFetchLog.fetchTimeEnd = DateTime.Now; entityLogistics.SaveChanges(); return(false); } if (solarDateFrom.Length == 10) { solarDateFrom = solarDateFrom.Remove(0, 2); } solarDateFrom = solarDateFrom.Replace("/", ""); try { dateTo = Functions.solarToMiladi(solarDateTo); } catch (Exception ex) { SharedVariables.logs.Error("wagonInfoSeirHistory.readAndSaveWagonToDB.solarToMiladi,solarDateTo", ex); entryCustomersHistoryFetchLog.state = -1; entryCustomersHistoryFetchLog.fetchTimeEnd = DateTime.Now; entityLogistics.SaveChanges(); return(false); } if (solarDateTo.Length == 10) { solarDateTo = solarDateTo.Remove(0, 2); } solarDateTo = solarDateTo.Replace("/", ""); if (dateFrom > dateTo) { SharedVariables.logs.Error("wagonInfoSeirHistory.readAndSaveWagonToDB.solarToMiladi, SolarDateFrom should be lower than SolarDateTo"); entryCustomersHistoryFetchLog.state = -1; entryCustomersHistoryFetchLog.fetchTimeEnd = DateTime.Now; entityLogistics.SaveChanges(); return(false); } login lg = new login(); start: if (login.v_cookieCollection == null) { if (!lg.fnc_login()) { //cannot login entryCustomersHistoryFetchLog.state = -1; entryCustomersHistoryFetchLog.fetchTimeEnd = DateTime.Now; entityLogistics.SaveChanges(); return(false); } } Uri uri; uri = new Uri(this.v_url); string postData; int pageNumber = 1; wagonInfoSeirHistoryDataTable dt = new wagonInfoSeirHistoryDataTable(); do { HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(uri); //webRequest.Headers.Add("Accept-Language", "en-US,en;q=0.8"); webRequest.Proxy = null; webRequest.Method = "POST"; //webRequest.Headers.Add("Cache-Control", "no-cache"); webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.CookieContainer = new CookieContainer(); foreach (Cookie cookie in login.v_cookieCollection) { webRequest.CookieContainer.Add(cookie); } postData = "WagonNo=" + wagonNo.ToString() + "&From_Date=" + solarDateFrom.ToString() + "&To_Date=" + solarDateTo.ToLower() + "&Page=" + pageNumber.ToString(); SharedVariables.logs.Info(postData); byte[] formData = Encoding.ASCII.GetBytes(postData); webRequest.ContentLength = formData.Length; Stream streamRequest = webRequest.GetRequestStream(); streamRequest.Write(formData, 0, formData.Length); streamRequest.Flush(); streamRequest.Close(); HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); if (webResponse != null) { if (webResponse.ResponseUri == null || string.IsNullOrEmpty(webResponse.ResponseUri.AbsoluteUri)) { webResponse.Close(); entryCustomersHistoryFetchLog.state = -1; entryCustomersHistoryFetchLog.fetchTimeEnd = DateTime.Now; entityLogistics.SaveChanges(); return(false); } if (login.fnc_isLoginPage(webResponse, this.v_url)) { //we haven't loginned yet if (lg.fnc_login()) { goto start; } else { //cannot login inform or log !!!!! webResponse.Close(); entryCustomersHistoryFetchLog.state = -1; entryCustomersHistoryFetchLog.fetchTimeEnd = DateTime.Now; entityLogistics.SaveChanges(); return(false); } } string result; using (StreamReader rd = new StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding("windows-1256"))) { result = rd.ReadToEnd(); } webResponse.Close(); if (!string.IsNullOrEmpty(result)) { dt.Rows.Clear(); Functions.sb_fillDatatableWithHtmlTableId(result, "AutoNumber2", 2, 0, 0, 0, dt); if (dt.Rows.Count > 0) { this.sb_saveToDB(dt, cycleNumber); } } } pageNumber++; } while (dt.Rows.Count > 0); SharedVariables.logs.Info("End Fetch " + wagonNo + " from " + solarDateFrom + " to " + solarDateTo); entryCustomersHistoryFetchLog.state = 1; entryCustomersHistoryFetchLog.fetchTimeEnd = DateTime.Now; entityLogistics.SaveChanges(); return(true); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { SharedVariables.logs.Error("wagonInfoSeirHistory.readAndSaveWagonToDB.finalException Entity of type \"" + eve.Entry.Entity.GetType().Name + "\" in state \"" + eve.Entry.State + "\" has the following validation errors:"); foreach (var ve in eve.ValidationErrors) { SharedVariables.logs.Error("- Property: \"" + ve.PropertyName + "\", Error: \"" + ve.ErrorMessage + "\""); } } return(false); } catch (Exception ex) { SharedVariables.logs.Error("wagonInfoSeirHistory.readAndSaveWagonToDB.finalException", ex); return(false); } } }
public void readAndSaveWagonToDB(long wagonNo, string solarDateEnd, int cycleNumber) { try { try { DateTime date = Functions.solarToMiladi(solarDateEnd); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show("Error in date format", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); return; } if (solarDateEnd.Length == 10) { solarDateEnd = solarDateEnd.Remove(0, 2); } solarDateEnd = solarDateEnd.Replace("/", ""); login lg = new login(); start: if (login.v_cookieCollection == null) { if (!lg.fnc_login()) { //cannot login return; } } Uri uri; uri = new Uri(this.v_url + "?WagonNO=" + wagonNo + "&Date=" + solarDateEnd); HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(uri); //webRequest.Headers.Add("Accept-Language", "en-US,en;q=0.8"); webRequest.Proxy = null; webRequest.Method = "GET"; //webRequest.Headers.Add("Cache-Control", "no-cache"); webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.CookieContainer = new CookieContainer(); foreach (Cookie cookie in login.v_cookieCollection) { webRequest.CookieContainer.Add(cookie); } HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); if (webResponse != null) { if (webResponse.ResponseUri == null || string.IsNullOrEmpty(webResponse.ResponseUri.AbsoluteUri)) { webResponse.Close(); return; } if (login.fnc_isLoginPage(webResponse, this.v_url)) { //we haven't loginned yet if (lg.fnc_login()) { goto start; } else { //cannot login inform or log !!!!! webResponse.Close(); return; } } string result; using (StreamReader rd = new StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding("windows-1256"))) { result = rd.ReadToEnd(); } webResponse.Close(); if (!string.IsNullOrEmpty(result)) { this.v_dt.Rows.Clear(); Functions.sb_fillDatatableWithHtmlTableId(result, "AutoNumber2", 2, 0, 0, 0, this.v_dt); if (this.v_dt.Rows.Count > 0) { this.sb_saveToDB(this.v_dt, cycleNumber); } } } } catch (Exception ex) { } }
public void readAndSaveWagonToDB(long wagonNoStart, long wagonNoEnd, int cycleNumber, bool get30DaysBeforeInformation) { wagonInfoTrackingDetail wagonDetail = new wagonInfoTrackingDetail(); try { login lg = new login(); start: if (login.v_cookieCollection == null) { if (!lg.fnc_login()) { //cannot login return; } } Uri uri; uri = new Uri(this.v_url); HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(uri); //webRequest.Headers.Add("Accept-Language", "en-US,en;q=0.8"); webRequest.Proxy = null; webRequest.Method = "POST"; //webRequest.Headers.Add("Cache-Control", "no-cache"); webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.CookieContainer = new CookieContainer(); foreach (Cookie cookie in login.v_cookieCollection) { webRequest.CookieContainer.Add(cookie); } string postData; postData = "FToolNo=" + wagonNoStart.ToString() + "&TToolNo=" + wagonNoEnd.ToString() + "&WNumber=&Wagon_No=&From_Date=&To_Date=&Rd_Exportkind=html"; byte[] formData = Encoding.ASCII.GetBytes(postData); webRequest.ContentLength = formData.Length; Stream streamRequest = webRequest.GetRequestStream(); streamRequest.Write(formData, 0, formData.Length); streamRequest.Flush(); streamRequest.Close(); HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); if (webResponse != null) { if (webResponse.ResponseUri == null || string.IsNullOrEmpty(webResponse.ResponseUri.AbsoluteUri)) { webResponse.Close(); return; } if (login.fnc_isLoginPage(webResponse, this.v_url)) { //we haven't loginned yet if (lg.fnc_login()) { goto start; } else { //cannot login inform or log !!!!! webResponse.Close(); return; } } string result; using (StreamReader rd = new StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding("windows-1256"))) { result = rd.ReadToEnd(); } webResponse.Close(); if (!string.IsNullOrEmpty(result)) { this.v_dt.Rows.Clear(); Functions.sb_fillDatatableWithHtmlTableId(result, "AutoNumber2", 2, 0, 0, 0, this.v_dt); if (this.v_dt.Rows.Count > 0) { this.sb_saveToDB(this.v_dt, cycleNumber); if (get30DaysBeforeInformation) { for (int i = 0; i <= this.v_dt.Rows.Count - 1; i++) { wagonDetail.readAndSaveWagonToDB(long.Parse(this.v_dt.Rows[i].ToString()), Functions.miladiToSolar(DateTime.Now), cycleNumber); } } } } } } catch (Exception ex) { } }
/// <summary> /// /// </summary> /// <param name="billOfLadingRange">1-10 or 1,2,3,4,5,6,7,8,9,10</param> /// <param name="cycleNumber"></param> /// <param name="get30DaysBeforeInformation"></param> public void readAndSaveWagonToDB(string billOfLadingRange, int cycleNumber) { try { string postData; #region check and assign post parameters if (string.IsNullOrEmpty(billOfLadingRange)) { throw new Exception("billOfLadingRange is not specified"); } int i, temp1, temp2; if (billOfLadingRange.Contains("-")) { //we have 1-10 var billOfLadingRangeArr = billOfLadingRange.Split('-'); if (billOfLadingRangeArr.Length != 2) { throw new Exception("billOfLadingRange is not specified correctly(" + billOfLadingRange + ")"); } if (!int.TryParse(billOfLadingRangeArr[0], out temp1) || !int.TryParse(billOfLadingRangeArr[1], out temp2)) { throw new Exception("billOfLadingRange is not specified correctly(" + billOfLadingRange + ")"); } if (temp1 > temp2) { throw new Exception("StartNumber of billOfLadingRange is greater than EndNumber (" + billOfLadingRange + ")"); } if (temp2 - temp1 > 20) { throw new Exception("EndNumber - StartNumber >20 (" + billOfLadingRange + ")"); } postData = "FBarnameNo=" + temp1.ToString() + "&TBarnameNo=" + temp2.ToString() + "&BNumber=&Barname_No="; } else { //we have 1,2,3,4,5,6,7,8,9,10 var billOfLadingsArr = billOfLadingRange.Split(','); if (billOfLadingsArr.Length > 20) { throw new Exception("You can specify atleast 20 numbers which is seperated by , (" + billOfLadingRange + ")"); } for (i = 0; i <= billOfLadingsArr.Length - 1; i++) { if (!int.TryParse(billOfLadingsArr[i], out temp1)) { throw new Exception("billOfLadingRange is not specified correctly(" + billOfLadingsArr[i] + ")"); } } postData = "FBarnameNo=&TBarnameNo=&BNumber=&Barname_No=" + billOfLadingRange; } #endregion login lg = new login(); start: if (login.v_cookieCollection == null) { if (!lg.fnc_login()) { //cannot login return; } } Uri uri; uri = new Uri(this.v_url); HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(uri); //webRequest.Headers.Add("Accept-Language", "en-US,en;q=0.8"); webRequest.Proxy = null; webRequest.Method = "POST"; //webRequest.Headers.Add("Cache-Control", "no-cache"); webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.CookieContainer = new CookieContainer(); foreach (Cookie cookie in login.v_cookieCollection) { webRequest.CookieContainer.Add(cookie); } byte[] formData = Encoding.ASCII.GetBytes(postData); webRequest.ContentLength = formData.Length; Stream streamRequest = webRequest.GetRequestStream(); streamRequest.Write(formData, 0, formData.Length); streamRequest.Flush(); streamRequest.Close(); HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); if (webResponse != null) { if (webResponse.ResponseUri == null || string.IsNullOrEmpty(webResponse.ResponseUri.AbsoluteUri)) { webResponse.Close(); return; } if (login.fnc_isLoginPage(webResponse, this.v_url)) { //we haven't loginned yet if (lg.fnc_login()) { goto start; } else { //cannot login inform or log !!!!! webResponse.Close(); return; } } string result; using (StreamReader rd = new StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding("windows-1256"))) { result = rd.ReadToEnd(); } webResponse.Close(); if (!string.IsNullOrEmpty(result)) { this.v_dt.Rows.Clear(); Functions.sb_fillDatatableWithHtmlTableId(result, "AutoNumber2", 2, 0, 0, 0, this.v_dt); if (this.v_dt.Rows.Count > 0) { this.sb_saveToDB(this.v_dt, cycleNumber); } } } } catch (Exception ex) { } }