下次是到网站上去获取一张图片,第一次是能成功获取的,但第二次按button3时,却提示Vacode.bmp被另一进程占用,大伙帮忙看看是什么大问? public static bool DowloadCheckImg(string Url)
{
bool bol = true;
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback((a, b, c, d) => { return true; });
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
//属性配置
webRequest.AllowWriteStreamBuffering = true;
webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
webRequest.MaximumResponseHeadersLength = -1;
webRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "GET";
webRequest.Headers.Add("Accept-Language", "zh-cn");
webRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
webRequest.KeepAlive = true;
CookieContainer cc = new CookieContainer();
webRequest.CookieContainer = cc;
try
{
//获取服务器返回的资源
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
using (Stream stream = webResponse.GetResponseStream())
{
List<byte> list = new List<byte>();
while (true)
{
int data = stream.ReadByte();
if (data == -1)
break;
list.Add((byte)data);
}
File.WriteAllBytes("D:\\Image\\Vacode.bmp", list.ToArray());
}
}
}
catch (WebException ex)
{
bol = false;
MessageBox.Show(ex.Message);
}
catch (Exception ex)
{
bol = false;
MessageBox.Show(ex.Message);
}
//Image numPic = Image.FromFile("D:\\Image\\Vacode.bmp");
//unCodeAiYing UnCheckobj = new unCodeAiYing((Bitmap)numPic);
//string strNum = UnCheckobj.getPicnum();
return bol; }
private void button3_Click(object sender, EventArgs e)
{
pictureBox1.Image = this.BackgroundImage;
DowloadCheckImg("https://......");
pictureBox1.Image = Image.FromFile("D:\\Image\\Vacode.bmp");
}
{
bool bol = true;
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback((a, b, c, d) => { return true; });
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
//属性配置
webRequest.AllowWriteStreamBuffering = true;
webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
webRequest.MaximumResponseHeadersLength = -1;
webRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "GET";
webRequest.Headers.Add("Accept-Language", "zh-cn");
webRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
webRequest.KeepAlive = true;
CookieContainer cc = new CookieContainer();
webRequest.CookieContainer = cc;
try
{
//获取服务器返回的资源
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
using (Stream stream = webResponse.GetResponseStream())
{
List<byte> list = new List<byte>();
while (true)
{
int data = stream.ReadByte();
if (data == -1)
break;
list.Add((byte)data);
}
File.WriteAllBytes("D:\\Image\\Vacode.bmp", list.ToArray());
}
}
}
catch (WebException ex)
{
bol = false;
MessageBox.Show(ex.Message);
}
catch (Exception ex)
{
bol = false;
MessageBox.Show(ex.Message);
}
//Image numPic = Image.FromFile("D:\\Image\\Vacode.bmp");
//unCodeAiYing UnCheckobj = new unCodeAiYing((Bitmap)numPic);
//string strNum = UnCheckobj.getPicnum();
return bol; }
private void button3_Click(object sender, EventArgs e)
{
pictureBox1.Image = this.BackgroundImage;
DowloadCheckImg("https://......");
pictureBox1.Image = Image.FromFile("D:\\Image\\Vacode.bmp");
}
线程操作,关闭
r.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0) Gecko/20100101 Firefox/7.0";
HttpWebResponse res = r.GetResponse() as HttpWebResponse;
Stream f = res.GetResponseStream();
byte[] buffer = new byte[32 * 1024];
int bytesProcessed = 0;
System.IO.FileStream fs = System.IO.File.Create("C:\\a.jpg");
int bytesRead;
do
{
bytesRead = f.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, bytesRead);
bytesProcessed += bytesRead;
}
while (bytesRead > 0);
fs.Flush();
fs.Close();
res.Close();
还有两个问题不明白
第一,不是说用using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
就不需要在XX.close();第二, 在我这个例子里,,我在两个地方加了几处 close,但是依然无效. try
{
//获取服务器返回的资源
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
using (Stream stream = webResponse.GetResponseStream())
{
List<byte> list = new List<byte>();
while (true)
{
int data = stream.ReadByte();
if (data == -1)
break;
list.Add((byte)data);
}
File.WriteAllBytes("D:\\Image\\Vacode.bmp", list.ToArray());
stream.Flush();
stream.Close();
stream.Dispose();
}
webResponse.Close(); }
}
没有意义。不需要使用using的时候就不要使用using,使用它反而画蛇添足,GC会去执行dispose的,用不着你去写using。
是不是被攻击了又?