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

來源:互聯網
上載者:User
HttpWebRequest使用注意(發生阻塞的解決辦法)

 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();
                }
            }

大家看下這段程式,有問題嗎?乍一看,好像沒有什麼問題,所有的流都釋放了,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執行個體在不需要的時候及時釋放資源。這樣可以重複使用而不會阻塞。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.