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

数据采集手工验证码老错误
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那来的。
------解决方案--------------------
你会断点不???

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


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


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