Esempio n. 1
0
        public void readAndSaveWagonToDB(long wagonNo, string solarDateFrom, string solarDateTo, int cycleNumber)
        {
            wagonInfoTrackingDetail wagonDetail = new wagonInfoTrackingDetail();

            try
            {
                DateTime dateFrom, dateTo;
                dateFrom = DateTime.MinValue;
                dateTo   = DateTime.MaxValue;
                try
                {
                    dateFrom = Functions.solarToMiladi(solarDateFrom);
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show("Error in date format solarDateFrom", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    return;
                }
                if (solarDateFrom.Length == 10)
                {
                    solarDateFrom = solarDateFrom.Remove(0, 2);
                }
                solarDateFrom = solarDateFrom.Replace("/", "");

                try
                {
                    dateTo = Functions.solarToMiladi(solarDateTo);
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show("Error in date format solarDateTo", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    return;
                }
                if (solarDateTo.Length == 10)
                {
                    solarDateTo = solarDateTo.Remove(0, 2);
                }
                solarDateTo = solarDateTo.Replace("/", "");

                if (dateFrom > dateTo)
                {
                    System.Windows.Forms.MessageBox.Show("SolarDateFrom should be lower than SolarDateTo", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    return;
                }
                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);
                string postData;
                int    pageNumber = 1;
                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();
                    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);
                            }
                        }
                    }
                    pageNumber++;
                } while (this.v_dt.Rows.Count > 0);
            }
            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)
            {
            }
        }
        public void readAndSaveToDB(int cycleNumber)
        {
            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 = "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();
                        trainPassengersLastStatus.sb_fillDatatableWithHtmlTable(result, 2, 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 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)
            {
            }
        }