如何优化这个HTTP连接
本文关键字:HTTP 连接 优化 何优化 | 更新日期: 2023-09-27 18:15:46
我是c#编程的新手,我正试图了解如何优化这段代码,它总是被调用到一个线程中以从IP摄像机获取图像(快照)。我试图做一些优化,使更快的HTTP请求,但我看不到如何和在哪里行动。我可以问一下专家是否有办法得到这个结果吗?谢谢你。
private Image GetImage()
{
HttpWebRequest req = null;
Image img = null;
HttpWebResponse res = null;
try
{
req = WebRequest.Create(_httpCommand) as HttpWebRequest;
req.Method = "GET";
req.PreAuthenticate = true;
req.AllowWriteStreamBuffering = true;
req.Credentials = new NetworkCredential(_user, _pwd);
res = req.GetResponse() as HttpWebResponse;
using (Stream rs = res.GetResponseStream())
{
img = new Bitmap(rs);
}
}
catch (Exception e) {
_status = false; //<<------------------Signaling the FAILURE status! ---
if (img != null)
img.Dispose(); //<< for ------GC--------------
//insert in LOG
Log logItem = new Log(DateTime.Now, e.Message);
AddLogItem(logItem);
}
return img;
}
我记得,HttpWebRequest
将尝试自动确定代理设置,这需要时间。
req.Proxy = GlobalProxySelection.GetEmptyWebProxy();
告诉它不要使用任何代理,因此搜索正确的设置将被跳过。
- http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.proxy%28v=vs.110%29.aspx