当前位置: 代码迷 >> C# >> 关于winform中webrequest与webrespomse网页爬虫登录的有关问题
  详细解决方案

关于winform中webrequest与webrespomse网页爬虫登录的有关问题

热度:63   发布时间:2016-05-05 03:24:14.0
关于winform中webrequest与webrespomse网页爬虫登录的问题
1、我已经找到post提交的格式

"UserName="+txtUserName.Text+"&UserPass="+txtPassword.Text+"&Verify="+txtCode.Text;

2、验证码已经获取,得到图片自己输入

CookieContainer cookies = new CookieContainer();
            string url = "http://www.xxxxxxxx.xxx/inc/getcode.asp?0.06453473589795022";  //验证码页面
            string rand = (new Random()).NextDouble().ToString();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Accept = "*/*";
            request.Method = "GET";
            request.UserAgent = "Mozilla/5.0";
            request.CookieContainer = new CookieContainer(); //暂存到新实例
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            MemoryStream ms = null;
            using (var stream = response.GetResponseStream())
            {
                Byte[] buffer = new Byte[response.ContentLength];
                int offset = 0, actuallyRead = 0;
                do
                {
                    actuallyRead = stream.Read(buffer, offset, buffer.Length - offset);
                    offset += actuallyRead;
                }
                while (actuallyRead > 0);
                ms = new MemoryStream(buffer);
            }
response.Close();
cookies = request.CookieContainer; //保存cookies
            //cookies.Add(response.Cookies);
            string strCookies = request.CookieContainer.GetCookieHeader(request.RequestUri); //把cookies转换成字符串
            //response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);


            pictureBox1.Image = Image.FromStream(ms);

3、在模拟提交时,如果没输入验证码,将提示未输入验证码,如果有输入,则提示验证码错误,是什么问题,求大虾解决
新手求助
下面附上截图,以便直观的了解,现在做登录这一块卡住做不下去,跪求

------解决思路----------------------
用webbrowser打开登录页面,把验证码复制出来,然后再摸拟提交
------解决思路----------------------
感觉你代码里面cookie没有弄好呢。。。
你应该在,定义一个全局的CookieContainer? cc
每次请求,先给request赋值,
每次得到Response,把值取出来,继续加入cc
------解决思路----------------------
关键是你代码太少。给你一个参考吧。你改改替换试试。


request.CookieContainer = cc;
            request.Method = "POST";    //使用post方式发送数据
            request.Timeout = 60000;
            if (string.IsNullOrEmpty(contentType))
            {
                request.ContentType = "application/x-www-form-urlencoded";
            }
            else
            {
                request.ContentType = contentType;
            }

            request.Referer = referer;
            request.AllowAutoRedirect = allowAutoRedirect;
            request.ContentLength = data.Length;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            //request.UnsafeAuthenticatedConnectionSharing = false;
            //模拟一个UserAgent
            Stream newStream = request.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();

            //获取网页响应结果
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            cc.Add(response.Cookies);


cc是全局的CookieContainer 对象。
还有就是你自己再多看看抓的包呢。。。cookie最好不要弄成字符串,容易出错。
------解决思路----------------------
你输入的验证码为空。  你postdata  里面没带上验证码。 
你把你的postdata附近的代码发出来看看。
  相关解决方案