Click here to Skip to main content
15,910,210 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public string HttpGet(string URI)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URI);
            req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            
            req.Accept = "*/*";
            req.Headers.Add("UA-CPU", "x86");
            req.Headers.Add("Accept-Language", "en-us");
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;  SV1; .NET CLR 1.1.4322)";
            
           
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            
            //Read all headers from the response & Request
            WebHeaderCollection ResponseHeader = resp.Headers;
            WebHeaderCollection RequestHeader = req.Headers;
            StreamReader sr = new StreamReader(resp.GetResponseStream());

            string s = URI;
            string s1 = req.RequestUri + " / " + (int)resp.StatusCode + " / " + resp.StatusDescription + Environment.NewLine + ResponseHeader.ToString() + sr.ReadToEnd();
            string s2 = req.RequestUri + " /" + req.Method + " / " + req.ProtocolVersion + " / " + Environment.NewLine + RequestHeader.ToString();

            NetworkMonitor(s, s1, s2);
            
            return s1.Trim();

        }


private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
           // startextraction();
            
            BackgroundWorker worker = (BackgroundWorker)sender;
            try
            {
                using (StreamReader reader = new StreamReader("F://testing.txt"))
                {
                    int out_count = 0;
                    int out_count_1 = 0;
                    int out_count_2 = 0;
                    int row_count = 0;

                    DataTable table = new DataTable();
                    foreach (DataRow rw in table_1.Rows)
                    {
                        table.Columns.Add(rw[0].ToString());
                    }
                    string input;
                    int i = 0;
                  
                    while ((input = reader.ReadLine()) != null)
                    {
                        string sourcecode = HttpGet(input);
                        worker.ReportProgress(i);

                        //string sourcecode = HttpPost(input, PostRaw);

                        //run_logs.AppendText("[Traversed] " + input + DateTime.Now.ToString());
                        //run_logs.AppendText(Environment.NewLine);
                        //run_logs.Update();
                       
                        // Here we check the Match instance.

                        foreach (DataRow row in table_1.Rows)
                        {
                            int index = table_1.Rows.IndexOf(row);

                            //Here we call Regex.Match.
                            Match match = Regex.Match(sourcecode, row[1].ToString(), RegexOptions.IgnoreCase);
                            int count = 0;
                            while (match.Success)
                            {
                                // Finally, we get the Group value and display it.
                                if (index == 0)
                                {
                                    table.Rows.Add(match.Groups[Convert.ToInt32(row[2].ToString())].Value);
                                    row_count++;
                                }
                                else if (index == 1)
                                {
                                    try
                                    {
                                        table.Rows[out_count][index] = match.Groups[Convert.ToInt32(row[2].ToString())].Value;
                                    }
                                    catch
                                    {
                                        table.Rows.Add();
                                    }
                                    finally
                                    {
                                        table.Rows[out_count][index] = match.Groups[Convert.ToInt32(row[2].ToString())].Value;
                                    }
                                    out_count++;
                                }
                                else if (index == 2)
                                {
                                    try
                                    {
                                        table.Rows[out_count_1][index] = match.Groups[Convert.ToInt32(row[2].ToString())].Value;
                                    }
                                    catch
                                    {
                                        table.Rows.Add();
                                    }
                                    finally
                                    {
                                        table.Rows[out_count_1][index] = match.Groups[Convert.ToInt32(row[2].ToString())].Value;
                                    }
                                    out_count_1++;
                                }
                                else if (index == 3)
                                {
                                    try
                                    {
                                        table.Rows[out_count_2][index] = match.Groups[Convert.ToInt32(row[2].ToString())].Value;
                                    }
                                    catch
                                    {
                                        table.Rows.Add();
                                    }
                                    finally
                                    {
                                        table.Rows[out_count_2][index] = match.Groups[Convert.ToInt32(row[2].ToString())].Value;
                                    }
                                    out_count_2++;
                                }
                                else
                                {

                                }
                                count++;
                                match = match.NextMatch();
                            }

                          //  run_logs.AppendText("[REGEX] " + regex + " Extracted " + count + " Matches");
                           // run_logs.AppendText(Environment.NewLine);
                            
                        }
                        //toolStripProgressBar1.Maximum = 4;
                        //toolStripProgressBar1.Value = toolStripProgressBar1.Value + 1;
                       // run_logs.Update();
                        dataGridView1.DataSource = table;
                        dataGridView1.Update();
                        dataGridView1.FirstDisplayedScrollingRowIndex = row_count;
                        
                    }

                }
            }
            catch (Exception hg)
            {
                MessageBox.Show(hg.Message);

            }
        }
Posted
Updated 8-Dec-13 9:11am
v2
Comments
CPallini 8-Dec-13 15:12pm    
Where's the problem? I'm able to see just a code dump.
Abhijeet pratap singh 8-Dec-13 15:20pm    
the problem is Cross-thread operation not valid: Control 'dataGridView1' accessed from a thread other than the thread it was created on

Quote:
the problem is Cross-thread operation not valid: Control 'dataGridView1' accessed from a thread other than the thread it was created on
This is a well-know problem, you may find a lot of info just Googling for. Morevover, Microsoft gently provides the guidelines for solving it: How to: Make Thread-Safe Calls to Windows Forms Controls[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Dec-13 18:41pm    
Good link, a 5.
—SA
Assign the result to e.result and update the UI in RunWorkerCompleted.

private void backgroundWorker1_DoWork(object sender,
DoWorkEventArgs e)
{

//Your code
.
.
.
.

// Assign the result DataTable table to e.Result in _DoWork
// to the Result property of the DoWorkEventArgs
// object. This is will be available to the
// RunWorkerCompleted eventhandler.
e.Result = table;
}

// This event handler deals with the results of the
// background operation.
private void backgroundWorker1_RunWorkerCompleted(
object sender, RunWorkerCompletedEventArgs e)
{
// First, handle the case where an exception was thrown.
if (e.Error != null)
{
// Handle Error
MessageBox.Show(e.Error.Message);
}
else if (e.Cancelled)
{
// Next, handle the case where the user canceled
// the operation.
// Note that due to a race condition in
// the DoWork event handler, the Cancelled
// flag may not have been set, even though
// CancelAsync was called.


}
else
{
// Finally, handle the case where the operation
// succeeded.
DataTable table= (DataTable) e.Result;

dataGridView1.DataSource = table;
dataGridView1.Update();
dataGridView1.FirstDisplayedScrollingRowIndex = table.Rows.Count;
}

}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900