http://my.chinahr.com/validCode.aspx 
这个地址是验证码的图片地址我要获取这个验证码.刚开始直接用<img src="http://my.chinahr.com/validCode.aspx"> 这样在IE下不能显示,在Opera,Firefox下都可以正常显示.不是浏览器问量,在他的网站上面可以正常显示,单独输入http://my.chinahr.com/validCode.aspx 也可以正常显示.后来用模拟提交方式来读取图片流,结果ContentLength=-1,读取不到.代码如下://HttpWebHelper 类using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading; public class HttpWebHelper
    {
      public Stream GetStreamByBytes(string server, string URL, byte[] byteRequest, string cookie,
                                out string header)
        {
            Stream stream = new MemoryStream(GetChinahrHtmlByBytes(server, URL, byteRequest, cookie, out header));
            return stream;
        }        public byte[] GetHtmlByBytes(string server, string URL, byte[] byteRequest, string cookie, out string header)
        {
            long contentLength;
            HttpWebRequest httpWebRequest;
            HttpWebResponse webResponse;
            Stream getStream;            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
            CookieContainer co = new CookieContainer();            httpWebRequest.CookieContainer = this._cc;            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            httpWebRequest.Accept =
                "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
            
            httpWebRequest.UserAgent = this.reqUserAgent;
            httpWebRequest.Method = "Post";
            httpWebRequest.ContentLength = byteRequest.Length;
            Stream stream;
            stream = httpWebRequest.GetRequestStream();
            stream.Write(byteRequest, 0, byteRequest.Length);
            stream.Close();
            webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            this._cc.Add(new Uri(server), webResponse.Cookies);
            header = webResponse.Headers.ToString();
            getStream = webResponse.GetResponseStream();
            contentLength = webResponse.ContentLength;            byte[] outBytes = new byte[contentLength];
            outBytes = ReadFully(getStream);
            getStream.Close();
            return outBytes;
        }        public byte[] ReadFully(Stream stream)
        {
            byte[] buffer = new byte[128];
            using (MemoryStream ms = new MemoryStream())
            {
                while (true)
                {
                    int read = stream.Read(buffer, 0, buffer.Length);
                    if (read <= 0)
                        return ms.ToArray();
                    ms.Write(buffer, 0, read);
                }
            }
        }
   }调用的地方 public void GetAuthCode()
        {
            HttpWebHelper helper = new HttpWebHelper();
            helper.SetProxy("192.168.255.8", 80, null, null);
            byte[] b = { };
            string header = "";
            string aspcookie = "";
            string html = helper.Get("http://www.chinahr.com/", Encoding.GetEncoding("utf-8"));
            Bitmap img = new Bitmap(helper.GetStreamByBytes("http://my.chinahr.com", "http://my.chinahr.com/ValidCode.aspx", b,
                aspcookie, out header));
            img.Save(Response.OutputStream, ImageFormat.Gif);
            Session["cookies"] = helper.GetCookie().GetCookies(new Uri("http://my.chinahr.com"));
           
        }
麻烦各位大牛看看.分不是问题,有点急,已经几天了.先谢谢了.

解决方案 »

  1.   

    public void GetAuthCode()
            {
                HttpWebHelper helper = new HttpWebHelper();
                helper.SetProxy("192.168.255.8", 80, null, null);
                byte[] b = { };
                string header = "";
                string aspcookie = "";
               //string html = helper.Get("http://www.chinahr.com/", Encoding.GetEncoding("utf-8"));
                Bitmap img = new Bitmap(helper.GetStreamByBytes("http://my.chinahr.com", "http://my.chinahr.com/ValidCode.aspx", b,
                    aspcookie, out header));
                img.Save(Response.OutputStream, ImageFormat.Gif);
                Session["cookies"] = helper.GetCookie().GetCookies(new Uri("http://my.chinahr.com"));
               
            }多了一句,这句不要.
      

  2.   

    <img src="http://my.chinahr.com/validCode.aspx">  =><img src="http://my.chinahr.com/validCode.aspx?date="+new Date()  />  
      

  3.   

    不能显示,在看下 你的电脑里面的 服务有个 “asp.net状态服务开启了没有".
    如果没有就他设置成自动就OK 了。
      

  4.   


    这种方式也不行 http://my.chinahr.com/validCode.aspx?r=0.0456487134 他网站的传值是这样的.
    现在的问题是在
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <img src="http://my.chinahr.com/validCode.aspx"/>
    </body>
    </html>这样都不能打开,但是直接输入地址就可以.
      

  5.   

    改成这样,加个时间:
    <img src="http://my.chinahr.com/validCode.aspx?date="+new Date() />   
      

  6.   


    网站不是我开发的网站,应该跟我的服务器没关系吧.
    直接输入http://my.chinahr.com/validCode.aspx这个地址是可以看到验证码的.
    而且在Firefox下面是可以用
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <img src="http://my.chinahr.com/validCode.aspx"/>
    </body>
    </html>
    是可以看到的.但是IE不行
      

  7.   

    那你就把 r=0.0456487134  也带上传递
    http://my.chinahr.com/validCode.aspx?r=0.0456487134&date=new date()
      

  8.   


    我觉得跟参数无关吧.在Firefox 下面可以打开,在IE面死活打不开
      

  9.   


    在IE下面加了参数也不显示图片,直接打叉,不加也不显示.在火狐下面加参数跟不加参数都会显示.您可以直接在浏览器里面打开http://my.chinahr.com/validCode.aspx看一下.就知道了这个我要抓取验证码的网站 http://my.chinahr.com
      

  10.   

    有没有什么容器在Web应用程序下使用
    类似桌面应用程下的WebBrowser容器?
      

  11.   

    你的真实目的其实是要采集到这张验证码图片是吧,可以请求时加上非ie浏览器的代理信息:
    比如用Opera浏览器的代理信息:httpWebRequest.UserAgent="Opera/9.80 (Windows NT 5.2; U; zh-cn) Presto/2.9.168 Version/11.50";
      

  12.   

    ie->工具->安全->可信站点->站点->输入“http://my.chinahr.com/validCode.aspx” 点击 添加按钮。ok
      

  13.   


    对,就是要图片就行了.我试过了,用的Firefox的代理信息
    long contentLength;
                HttpWebRequest httpWebRequest;
                HttpWebResponse webResponse;
                Stream getStream;            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
                CookieContainer co = new CookieContainer();            httpWebRequest.CookieContainer = this._cc;            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                httpWebRequest.Accept =
                    "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                httpWebRequest.UserAgent = "User-Agent: Mozilla/5.0 (Windows NT 5.2; rv:7.0.1) Gecko/20100101 Firefox/7.0.1";
                httpWebRequest.Method = "Post";           
                httpWebRequest.ContentLength = byteRequest.Length;
                Stream stream;
                stream = httpWebRequest.GetRequestStream();
                stream.Write(byteRequest, 0, byteRequest.Length);
                stream.Close();
                webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                this._cc.Add(new Uri(server), webResponse.Cookies);
                header = webResponse.Headers.ToString();
                getStream = webResponse.GetResponseStream();
                contentLength = webResponse.ContentLength;            byte[] outBytes = new byte[contentLength];
                outBytes = ReadFully(getStream);
                getStream.Close();
                return outBytes;
      

  14.   

    是跨域问题。没办法的,谁让你借用了IE的作为HTTP客户端解析HTML呢