首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

数据采集手工验证码老异常

2012-04-26 
数据采集手工验证码老错误HttpWebResponse result nullstring jg txtResult.Text msg.Text

数据采集手工验证码老错误
HttpWebResponse result = null;
  string jg = "";
  txtResult.Text = "";
  msg.Text = "";

  string url = "http://jj.gdga.gov.cn/cx/wzss/wzss.do";
  string cph1 = txtCph.Text.Substring(0, 1);
  string cph2 = txtCph.Text.Substring(1, 6);
   
  string payload = "jc=" + cph1 + "&hphm=" + cph2 + "&hpzl=" + cl + "&lxdh=" + cj + "&method=查询&randcode2=" + code;

  try
  {

  HttpWebRequest req =(HttpWebRequest) WebRequest.Create(url);
  req.ContentType = "application/x-www-form-urlencoded";  
  req.Method = "POST";
  req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
  req.Expect = "Thu, 01 Jan 1970 00:00:00 GMT";
  StringBuilder UrlEncoded = new StringBuilder();
  Char[] reserved = { '?', '=', '&' };
  byte[] SomeBytes = null;

  if (payload != null)
  {
  int i = 0, j;
  while (i < payload.Length)
  {
  j = payload.IndexOfAny(reserved, i);
  if (j == -1)
  {
  UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, payload.Length - i)));
  break;
  }
  UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, j - i)));
  UrlEncoded.Append(payload.Substring(j, 1));
  i = j + 1;
  }
  SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
  req.ContentLength = SomeBytes.Length;
  Stream newStream = req.GetRequestStream();
  newStream.Write(SomeBytes, 0, SomeBytes.Length);
  newStream.Close();
  }
  else
  {
  req.ContentLength = 0;
  }


  result = req.GetResponse();
  Stream ReceiveStream = result.GetResponseStream();
  Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
  StreamReader sr = new StreamReader(ReceiveStream, encode);

  //msg.Text += "\r\n已接收到响应流";

  Char[] read = new Char[256];
  int count = sr.Read(read, 0, 256);
  msg.Text += "HTML...\r\n";
  while (count > 0)
  {
  String str = new String(read, 0, count);
  jg += str;
  count = sr.Read(read, 0, 256);
  }

  ReceiveStream.Close();
  sr.Close();
  msg.Text = jg;


上面就是我的代码,运行后取到的页面内容里面含有验证码错误,我不知道哪里出了问题,哪位朋友帮我讲解下,谢谢先了

[解决办法]
randcode2=" + code; code那来的。
[解决办法]
你会断点不???

断点看你取的内容就知道了啊。


验证码错误?难道你采集数据的页面需要输入验证码?
[解决办法]
你要采集的那个网站的某个页面上是不是要输入验证码???


网页上要求输入验证码,你程序去抓取,不会自动输入,就会报错 “验证码错误!!”



然后你就会把这个抓取了。
[解决办法]
POST /cx/wzss/wzss_jsy.do HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://jj.gdga.gov.cn/cx/wzss/wzss.do
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; InfoPath.2)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: jj.gdga.gov.cn
Content-Length: 87
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=DDE9DFFA84924F7B823E987698B6D170

method=%E6%9F%A5%E8%AF%A2&jszh=sduyyyuuyyu&xm=sdyuuy&randcode=8933&image.x=46&image.y=9

你用iehttpheaders 看看头部

热点排行