简体   繁体   中英

C# false http response

I have a thread that returns a site's http response status, but sometimes my program returns false results. and after a while it gives good results.
False result: it takes a big a mount of time to check, and then it says that (for example) Google is down, which is quite not reasonable, but after a few seconds it returns good results

Can you take a look and tell me whats wrong? or how I can I improve it?
Checks all sites in datagrid:

 private void CheckSites()
        {
            if (CheckSelected())
            {
                int rowCount = dataGrid.BindingContext[dataGrid.DataSource, dataGrid.DataMember].Count;
                string url;
                for (int i = 0; i < rowCount; i++)
                {
                    url = dataGrid.Rows[i].Cells[2].Value.ToString();
                    if (url != null)
                    {
                        Task<string[]> task = Task.Factory.StartNew<string[]>
                         (() => checkSite(url));

                        // We can do other work here and it will execute in parallel:
                        //Loading...

                        // When we need the task's return value, we query its Result property:
                        // If it's still executing, the current thread will now block (wait)
                        // until the task finishes:
                        string[] result = task.Result;
                        selectRows();
                        if (result[0] != System.Net.HttpStatusCode.OK.ToString() && result[0] != System.Net.HttpStatusCode.Found.ToString() && result[0] != System.Net.HttpStatusCode.MovedPermanently.ToString())
                        {
                            //bad
                            notifyIcon1.ShowBalloonTip(5000, "Site Down", dataGrid.Rows[i].Cells[2].Value.ToString() + ", has a status code of:" + result, ToolTipIcon.Error);
                            dataGrid.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Wheat;
                            TimeSpan ts;
                            TimeSpan timeTaken = TimeSpan.Parse(result[1]);
                            dataGrid.Rows[i].Cells[3].Value = result[0];
                            dataGrid.Rows[i].Cells[3].Style.BackColor = System.Drawing.Color.Red;
                            dataGrid.Rows[i].Cells[4].Value = timeTaken.Seconds.ToString() + "." + String.Format("{0:0.00000}", timeTaken.Milliseconds.ToString()) + " seconds.";
                            string sec = (DateTime.Now.Second < 10) ? "0" + DateTime.Now.Second.ToString() : DateTime.Now.Second.ToString();
                            string min = (DateTime.Now.Minute < 10) ? "0" + DateTime.Now.Minute.ToString() : DateTime.Now.Minute.ToString();
                            string hour = (DateTime.Now.Hour < 10) ? "0" + DateTime.Now.Hour.ToString() : DateTime.Now.Hour.ToString();
                            dataGrid.Rows[i].Cells[5].Value = hour + ":" + min + ":" + sec;

                            //loadbar
                        }
                        else if (result[0] == "catch")//catch
                        {
                            notifyIcon1.ShowBalloonTip(10000, "SITE DOWN", dataGrid.Rows[i].Cells[1].Value.ToString() + ", Error:" +result[1], ToolTipIcon.Error);
                            dataGrid.Rows[i].Cells[3].Value = result[1];
                            dataGrid.Rows[i].Cells[3].Style.BackColor = System.Drawing.Color.Red;
                            //loadbar

                        }
                        else
                        {
                            //good
                            TimeSpan timeTaken = TimeSpan.Parse(result[1]);
                            dataGrid.Rows[i].Cells[3].Value = result[0];
                            dataGrid.Rows[i].Cells[3].Style.BackColor = System.Drawing.Color.LightGreen;
                            dataGrid.Rows[i].Cells[4].Value = timeTaken.Seconds.ToString() + "." + String.Format("{0:0.00000}", timeTaken.Milliseconds.ToString()) + " seconds.";
                            string sec = (DateTime.Now.Second < 10) ? "0" + DateTime.Now.Second.ToString() : DateTime.Now.Second.ToString();
                            string min = (DateTime.Now.Minute < 10) ? "0" + DateTime.Now.Minute.ToString() : DateTime.Now.Minute.ToString();
                            string hour = (DateTime.Now.Hour < 10) ? "0" + DateTime.Now.Hour.ToString() : DateTime.Now.Hour.ToString();
                            dataGrid.Rows[i].Cells[5].Value = hour + ":" + min + ":" + sec;
                            //loadbar
                        }
                        selectRows();
                    }
                }
            }
        }

Checks a site:

  /////////////////////////////////
        ////Check datagrid websites-button - returns response
        /////////////////////////////////
        private string[] checkSite(string url)
        {
            string[] response = new string[2];
            url = dataGrid.Rows[0].Cells[2].Value.ToString();
            if (url != null)
            {
                try
                {
                    HttpWebRequest httpReq;


                    httpReq.Timeout = 10000;
                    //loadbar
                    dataGrid.Rows[0].DefaultCellStyle.BackColor = System.Drawing.Color.Wheat;
                    System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
                    timer.Start();
                    HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse(); //httpRes.Close();
                    timer.Stop();
                    //loadbar
                    HttpStatusCode httpStatus = httpRes.StatusCode;
                    response[0] = httpStatus.ToString();
                    response[1] = timer.Elapsed.ToString();//*
                    httpRes.Close();
                    return response;
                }
                catch (Exception he)
                {
                    response[0] = "catch";
                    response[1] = he.Message;
                    return response;
                }
            }
            response[0] = "catch";
            response[1] = "No URL entered";
            return response;
            //dataGrid.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Blue;

        }

Thanks in advance.

Assuming the code provided is the actual code used:

First of all, your definition of 'False result' and 'Good result' is wrong. If you expect A but get B, that doesn't mean B is invalid. If your wife is giving birth and you expect a boy but it turns out the be a girl, its not a false result. Just unexpected.

That said: lets analyze your work: If it takes a long long time to check a site only to finally get a ??? result which isn't a 200 response code. We can almost savely assume you are dealing with a timeout. If your router, google or any fundamental network device in between is having problems, its expected to get an unexpected answer. "Timeout", "Bad Request", "Server not available" etc. Why would this happen? Its impossible to say for certain without having direct access to your environment.

Looking at your code however, i see that you're using the default TaskScheduler for making each check run as a task in the background (assuming you havent changed the default task scheduler which would be a vey bad practice to begin with). The default task scheduler, schedules each task on the threadpool which results in many many tasks running simultanious. Here we have a good candidate for overloading your network. Many sites (esspecially google) are kinda sensitive for handling many requests from the same source (esspecially if the frequency is high) so maybe google is blocking you temporarily or holding you back. Again, at this point it's pure speculation but the fact that you're running all checks simultaniously (unless the thread pool is on his max) is very likely the cause of your problem.

UPDATE

I would recommend working with a LimitedConcurrencyTaskScheduler ( see here: http://blogs.msdn.com/b/pfxteam/archive/2010/04/09/9990424.aspx ). Here you can limit the amount of tasks that can be run asynchronously. You have to do some testing for what number works ideally in your situation. Also make sure that the frequency is not 'too' high. Its hard to define what is too high, only testing can proof that.

In order to simulate your scenario, I have created a Winform with data grid and a button. On load of the form, I programmatically creates list of url's (in a table) and bind to data grid. And on button click, we start the download process. In concise, the you have to write more defensive code and the following code only a skeleton of how you can fix the issue.

using System;
using System.Data;
using System.Net;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace app
{
    public partial class Form1 : Form
    {
        DataTable urls = new DataTable();
        public Form1()
        {
            InitializeComponent();
        }
        //Fill your uri's and bind to a data grid.
        void InitTable()
        {
            //Silly logic to simulate your scenario.
            urls = new DataTable();
            urls.Columns.Add(new DataColumn("Srl", typeof(string)));
            urls.Columns.Add(new DataColumn("Urls", typeof(Uri)));
            urls.Columns.Add(new DataColumn("Result", typeof(string)));

            DataRow dr = urls.NewRow();
            dr["Srl"] = "1";
            dr["Urls"] = new Uri("http://www.microsoft.com");
            dr["Result"] = string.Empty;
            urls.Rows.Add(dr);
            dr = urls.NewRow();
            dr["Srl"] = "2";
            dr["Urls"] = new Uri("http://www.google.com");
            dr["Result"] = string.Empty;
            urls.Rows.Add(dr);
            dr = urls.NewRow();
            dr["Srl"] = "3";
            dr["Urls"] = new Uri("http://www.stackoverflow.com");
            dr["Result"] = string.Empty;
            urls.Rows.Add(dr);
            urls.AcceptChanges();
        }
        void UpdateResult()
        {
            dataGridView1.DataSource = urls;
        }

        //Important
        // This example will freeze UI. You can avoid this while implementing
        //background worker or pool with some event synchronization. I haven't covered those area since
        //we are addressing different issue. Let me know if you would like to address UI freeze
        //issue. Or can do it your self.
        private void button1_Click(object sender, EventArgs e)
        {            
            //Create array for Task to parallelize multiple download.
            var tasks = new Task<string[]>[urls.Rows.Count];
            //Initialize those task based on number of Uri's
            for(int i=0;i<urls.Rows.Count;i++)
            {
                int index = i;//Do not change this. This is to avoid data race
                //Assign responsibility and start task.
                tasks[index] = new Task<string[]>(
                        () => checkSite(
                            new TaskInput(urls.Rows[index]["Urls"].ToString(), urls.Rows[index]["Srl"].ToString())));
                tasks[index].Start();

            }
            //Wait for all task to complete. Check other overloaded if interested.
            Task.WaitAll(tasks);

            //block shows how to access result from task
            foreach (var item in tasks)
            {
               DataRow[] rows=urls.Select("Srl='"+item.Result[2]+"'");
               foreach (var row in rows)
                      row["Result"]=item.Result[0]+"|"+item.Result[1];
            }
            UpdateResult();
        }
        //This is dummy method which in your case 'Check Site'. You can have your own
        string[] checkSite(TaskInput input)
        {
            string[] response = new string[3];
            if (input != null)
            {
                try
                {
                    WebResponse wResponse = WebRequest.Create(input.Url).GetResponse();

                    response[0] = wResponse.ContentLength.ToString();
                    response[1] = wResponse.ContentType;
                    response[2] = input.Srl;
                    return response;
                }
                catch (Exception he)
                {
                    response[0] = "catch";
                    response[1] = he.Message;
                   response[2] = input.Srl;
                    return response;
                }
            }
            response[0] = "catch";
            response[1] = "No URL entered";
            response[2] = input.Srl;
            return response;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitTable();
            UpdateResult();
        }
    }
    //Supply custom object for simplicity
    public class TaskInput
    {
       public TaskInput(){}
       public TaskInput(string url, string srl)
        {
            Url = url;
            Srl = srl;
        }
        public string Srl { get; set; }
        public string Url { get; set; }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM