HttpWebRequest times out on second call

后端 未结 8 557
猫巷女王i
猫巷女王i 2020-11-29 03:45

Why does the following code Timeout the second (and subsequent) time it is run?

The code hangs at:

using (Stream objStream = request.GetResponse().Ge         


        
8条回答
  •  囚心锁ツ
    2020-11-29 04:28

    I read the post tagged as answer above and it didn't really work for me above. I ended up rewriting the code using the using tags and added a CloseConnectionGroup segment someone else recommended here. Ultimately this is what the code looks like:

    System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(imageUrl);
                    webRequest.AllowWriteStreamBuffering = true;
                    webRequest.Timeout = 30000;
                    webRequest.ServicePoint.ConnectionLeaseTimeout = 5000;
                    webRequest.ServicePoint.MaxIdleTime = 5000;
    
                    using (System.Net.WebResponse webResponse = webRequest.GetResponse())
                    {
    
                        using (System.IO.Stream stream = webResponse.GetResponseStream())
                        {
                            image = System.Drawing.Image.FromStream(stream);
                        }
                    }
    
                    webRequest.ServicePoint.CloseConnectionGroup(webRequest.ConnectionGroupName);
                    webRequest = null; 
                }
    

提交回复
热议问题