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也释放了。。不过如果你写个循环无限次发起请求,你会发现,运行不了几次就阻塞了。为什么呢?大家看下面的代码
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();
}
}
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();
}
}
多了些什么?多了这个
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实例在不需要的时候及时释放资源。这样可以重复使用而不会阻塞。
{
myRequest.Abort();
}
---------------------------------------------------------
专注移动开发
Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian