日期:2014-05-18  浏览次数:20650 次

获取网站验证码的图片文件
网站地址是http://58.27.8.3/
图片文件在http://58.27.8.3/img? 后面是一个随机数(script是用math.random()实现的)
请大家帮忙

------解决方案--------------------
再修改一下,那个上边没有即时关闭的流,其实这个抓取还是挺稳定的
C# code

String guid = Guid.NewGuid().ToString();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(String.Format("http://58.27.8.3/img.jpg?{0}", guid));
            //request.Referer = "http://www.XXXX.com"; //网址
            request.Timeout = Timeout.Infinite;
            request.UserAgent =
                "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; Maxthon; .NET CLR 1.1.4322; CIBA; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2)";
            request.Referer = "http://58.27.8.3/_index.jsp";
            
            CookieContainer cookieContainer = new CookieContainer(1);
            Cookie sessionId = new Cookie("JSESSIONID","19425294A3A50B5ED89919A516FBB516.node36");
            sessionId.Domain = "58.27.8.3";
            cookieContainer.Add(sessionId);

            request.CookieContainer = cookieContainer;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream stream = response.GetResponseStream();
            if(response.ContentLength == 0)
            {
                Console.WriteLine("sessionid无效");
                return;
            }
            //StreamReader reader = new StreamReader(stream);
            //Console.Write(reader.ReadToEnd());

            Image img = Image.FromStream(stream);
            FileStream fileStream = File.Open(String.Format("e:\\{0}.gif",guid), FileMode.OpenOrCreate);
            img.Save(fileStream, ImageFormat.Gif);
            fileStream.Flush();
            fileStream.Close();
            img.Dispose();
            response.Close();