I have a for loop that loops with a listbox. For each item in the listbox I want to perform a httpwebrequest. My loop is doing the first item then occasionally just iterates down the list not doing the httpwebrequest for the other items. Im not sure if theres not enough time between requests or something.

Maybe you can help. I removed my postdata and sites I request to on purpose:

listBox4.SetSelected(i, true);
                listBox5.SetSelected(i, true);
                listBox4.SelectedItem.ToString();

                string[] details = { listBox4.SelectedItem.ToString(), listBox5.SelectedItem.ToString() };

                foreach (string element in details)
                {
                    Thread.Sleep(400);


                    System.Net.ServicePointManager.Expect100Continue = false;

                    string postdata = "";


                    UTF8Encoding encoding1 = new UTF8Encoding();
                    byte[] bytedata = encoding1.GetBytes(postdata);

                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("");
                    request.Method = "POST";
                    request.KeepAlive = false;
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.Referer = "";
                    request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6";
                    request.ContentLength = bytedata.Length;



                    Stream postreqstream2 = request.GetRequestStream();
                    postreqstream2.Write(bytedata, 0, bytedata.Length);
                    postreqstream2.Close();
                    HttpWebResponse response = default(HttpWebResponse);

                    response = (HttpWebResponse)request.GetResponse();
                    StreamReader reader2 = new StreamReader(response.GetResponseStream());



                    string twitter = reader2.ReadToEnd();
                    richTextBox1.Text = twitter;
                    webBrowser1.DocumentText = (richTextBox1.Text);



                    toolStripStatusLabel1.Text = "Idle";
listBox6.Items.Add(listBox5.SelectedItem.ToString() + ":" + txtBoxImportPasswords.Text + "\r");


                    reader2.Close();
                    reader2.Dispose();

Thanks I will look at the link

Alright I saw it and came back with this code:

HttpWebRequest webRequest;

void StartWebRequest()
{
    webRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), null);
}

void FinishWebRequest(IAsyncResult result)
{
    webRequest.EndGetResponse(result);
}

Where do I add it? In my httpwebrequest, above it? Also is the code copy/paste-able? Do I need to edit anything in my httpwebrequest? Also does my httpwebrequest need to be inside a method?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.