1. 程式人生 > >HttpWebRequest使用注意(發生阻塞的解決辦法)

HttpWebRequest使用注意(發生阻塞的解決辦法)

HttpWebRequest使用注意(發生阻塞的解決辦法)

           HttpWebResponse myResponse = null;
            Stream reqStream = null; 
            Stream resStream = null;

            try
            {
                byte[] data = System.Text.Encoding.Default.GetBytes(param);

                myRequest = (HttpWebRequest)WebRequest.Create(url);
                myRequest.Method 
= "POST"; myRequest.KeepAlive = true; myRequest.ContentType = "application/octet-stream"; myRequest.ContentLength = data.Length; reqStream = myRequest.GetRequestStream(); reqStream.Write(data, 0, data.Length); reqStream.Close(); myResponse
= (HttpWebResponse)myRequest.GetResponse(); resStream = myResponse.GetResponseStream(); data = new byte[512]; int count = 0; UIFactory.zZRK_MODIForm.memStream = new MemoryStream(); while ((count = resStream.Read(data, 0
, data.Length)) > 0) { UIFactory.zZRK_MODIForm.memStream.Write(data, 0, count); } resStream.Close(); } catch { } finally { if (resStream != null) { resStream.Close(); } if (reqStream != null) { reqStream.Close(); } if (myResponse != null) { myResponse.Close(); } }

 

大家看下這段程式,有問題嗎?乍一看,好像沒有什麼問題,所有的流都釋放了,Response也釋放了。。不過如果你寫個迴圈無限次發起請求,你會發現,執行不了幾次就阻塞了。為什麼呢?大家看下面的程式碼 

            HttpWebRequest myRequest = null;
            HttpWebResponse myResponse = null;
            Stream reqStream = null; 
            Stream resStream = null;

            try
            {
                byte[] data = System.Text.Encoding.Default.GetBytes(param);

                //想伺服器端傳送請求,獲取照片資訊
                myRequest = (HttpWebRequest)WebRequest.Create(url);
                myRequest.Method = "POST";
                myRequest.KeepAlive = true;
                myRequest.ContentType = "application/octet-stream";
                myRequest.ContentLength = data.Length;
                reqStream = myRequest.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();

                myResponse = (HttpWebResponse)myRequest.GetResponse();
                resStream = myResponse.GetResponseStream();
                data = new byte[512];
                int count = 0;
                UIFactory.zZRK_MODIForm.memStream = new MemoryStream();
                while ((count = resStream.Read(data, 0, data.Length)) > 0)
                {
                    UIFactory.zZRK_MODIForm.memStream.Write(data, 0, count);
                }
                resStream.Close();
                
            }
            catch
            {
            }
            finally
            {
                if (resStream != null)
                {
                    resStream.Close();
                }
                if (reqStream != null)
                {
                    reqStream.Close();
                }
                if (myResponse != null)
                {
                    myResponse.Close();
                }
                if (myRequest != null)
                {
                    myRequest.Abort();
                }
            }

 

多了些什麼?多了這個

if (myRequest != null)
{
myRequest.Abort();
}

 

其實很多時候釋放了Stream和Response還不夠,客戶端的Request還是在保持著,需要等垃圾回收器來回收,所以一般很容易阻塞,導致請求傳送不出去。加上這個就是讓HttpWebRequest例項在不需要的時候及時釋放資源。這樣可以重複使用而不會阻塞。