WebRequest.GetResponse锁起来?WebRequest、GetResponse

2023-09-02 10:49:58 作者:玩心眼子的年代"

在上的GetResponse写我下面的code锁了起来。为什么呢?

 尝试
        {
            WebRequest的myWebRequest = WebRequest.Create(strURL);
            WebResponse类myWebResponse = myWebRequest.GetResponse();
            //更多code在这里
 

解决方案

这通常发生,如果你做了一些请求到同一台主机,而不是被处置的 WebResponse类

默认的连接管理设置只允许2(或者4,我不记得了),打开连接到同一主机的时间。如果你的真正的需要改变这一点,使用<connectionManagement> app.config中的元素 - 但通常你会没事的只是处置 WebResponse类

 尝试
{
    WebRequest的myWebRequest = WebRequest.Create(strURL);
    使用(WebResponse类myWebResponse = myWebRequest.GetResponse())
    {
        //更多code在这里
 

When writing the below my code locks up on GetResponse. Why?

        try
        {
            WebRequest myWebRequest = WebRequest.Create(strURL);
            WebResponse myWebResponse = myWebRequest.GetResponse();
            //more code here

解决方案

This usually happens if you've made several requests to the same host, and not disposed of the WebResponse.

The default connection management settings only allow 2 (or maybe 4, I can't remember) open connections to the same host at a time. If you really need to change this, use the <connectionManagement> app.config element - but usually you'll be fine just disposing of WebResponse:

try
{
    WebRequest myWebRequest = WebRequest.Create(strURL);
    using (WebResponse myWebResponse = myWebRequest.GetResponse())
    {
        //more code here