Multithreading a large number of web requests in c#

前端 未结 4 759
星月不相逢
星月不相逢 2020-12-04 13:24

I have an program where I need to create some large number of folders to an external sharepoint site (external meaning I can\'t use the sharepoint object model). Web request

4条回答
  •  心在旅途
    2020-12-04 14:16

    try this

    folderPathList.ToList().ForEach(p =>
            {
                ThreadPool.QueueUserWorkItem((o) =>
                     {
                         WebRequest request = WebRequest.Create(p);
                         request.Credentials = DefaultCredentials;
                         request.Method = "MKCOL";
                         WebResponse response = request.GetResponse();
                         response.Close(); 
                     });
    

    EDIT - different webrequest approach

    folderPathList.ToList().ForEach(p =>
            {
                ThreadPool.QueueUserWorkItem((o) =>
                     {
                         using (WebClient client = new WebClient())
                         {
                             client.Credentials = DefaultCredentials;
                             client.UploadString(p, "MKCOL", "");
                         }
                     });
            });
    

提交回复
热议问题