Multiple HTTP request in C#

后端 未结 5 998
北海茫月
北海茫月 2021-02-15 01:56

I need to send about 200 HTTP requests in parallel to different servers and get response. I use HttpWebRequest class in C#. But I don\'t see good time enhancement when requests

相关标签:
5条回答
  • 2021-02-15 02:05

    Instead of starting up your own threads try using the asynchronous methods of HttpWebRequest such as HttpWebRequest.BeginGetResponse and HttpWebRequest.BeginGetRequestStream.

    0 讨论(0)
  • 2021-02-15 02:07


    you can try this :

    try
            {
                List<Uri> uris = new List<Uri>();
                uris.Add(new Uri("http://www.google.fr"));
                uris.Add(new Uri("http://www.bing.com"));
    
                Parallel.ForEach(uris, u =>
               {
                   WebRequest webR = HttpWebRequest.Create(u);
                   HttpWebResponse webResponse = webR.GetResponse() as HttpWebResponse;
               });
            }
            catch (AggregateException exc)
            {
                exc.InnerExceptions.ToList().ForEach(e =>
                    {
                        Console.WriteLine(e.Message);
                    });
            }
    
    0 讨论(0)
  • 2021-02-15 02:07

    This is the code of android application of sending the request. How can we use the upper code like:

    params.add(new BasicNameValuePair("key", "value");
    HttpPost request = new HttpPost();
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("key", "value");// How can we give the value in this format format
    post.setEntity(new UrlEncodedFormEntity(params));
    
    httpClient.execute(request);
    
    0 讨论(0)
  • 2021-02-15 02:18

    Use asynchronous web requests in stead.

    Edit: http://msdn.microsoft.com/en-us/library/86wf6409(VS.71).aspx

    0 讨论(0)
  • 2021-02-15 02:20

    This might help - http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/1f863f20-09f9-49a5-8eee-17a89b591007

    Suggests there is a limit on the number of TCP connections allowed, but that you can increase the limit

    0 讨论(0)
提交回复
热议问题