简体   繁体   中英

C# httpwebrequest with many request

i have list word i need to send all this word on webrequest POST method like here : this function to request>

   private void RequesteUrl(string word)
    {
     //   try
       // {


            CookieContainer WeBCookies = new CookieContainer();
            HttpWebRequest url = (HttpWebRequest)WebRequest.Create("http://mbc.com/index.php");
            url.Method = "POST";
            url.UserAgent = "Mozilla/5.0 (Windows NT 10.0; rv:44.0) Gecko/20100101 Firefox/44.0";
           url.Referer = "http://mbc.com/index.php?";

            var PostData =  User.Text + "&password=" + word;
            var Data = Encoding.ASCII.GetBytes(PostData);
            url.ContentLength = Data.Length;
            url.Host = "www.mbc.com";
            url.ContentType = "application/x-www-form-urlencoded";
          //  url.Headers.Add(HttpRequestHeader.Cookie, webBrowser1.Document.Cookie);
            url.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            url.KeepAlive = true;


            url.Headers.Add("Cookie: "+tryit);
            using (var stream = url.GetRequestStream())
            {
                stream.Write(Data, 0, Data.Length);
            }

            HttpWebResponse WebResponse = (HttpWebResponse)url.GetResponse();
            int status = (int)WebResponse.StatusCode;
            Stream ST = WebResponse.GetResponseStream();
            StreamReader STreader = new StreamReader(ST);
            string RR = STreader.ReadToEnd();


           // MessageBox.Show (status.ToString() + "   " + word);

    //    }
     //   catch (Exception ex)
     //   {
      //      MessageBox.Show(ex.Message);
     //   }

    }

and i use this to load :

 foreach (string selectword in Lists)
        {

            RequesteUrl(selectword);
  }

but foreach not complete all list , he do to two from list only !

Yes, the reason is that you are not releasing resources after your request is complete. Since HTTP/1.1 only allows two simultaneous connections per server, after two requests you will run out of available connections. That is why it hangs.

You need to close the HttpWebResponse, and also good idea to close the streamreader and stream....

using(HttpWebResponse WebResponse = (HttpWebResponse)url.GetResponse())
{
            int status = (int)WebResponse.StatusCode;

            using(Stream ST = WebResponse.GetResponseStream())
            using(StreamReader STreader = new StreamReader(ST))
                string RR = STreader.ReadToEnd();
}

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