HttpHelper有时候会报【本次请求并未返回任何数据】
这个怎么破?
我是读取人家网站的图片验证码 使用OCR解析,有没有了解这个的朋友帮忙分析下
[C#] 纯文本查看 复制代码 #region have a try
//try
string sCookie = string.Empty;
string _Referer = string.Empty;
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem()
{
URL = "http://www.ems.com.cn/ems/order/singleQuery_t",
Allowautoredirect = false,
Method = "GET",
};
HttpResult result = http.HttpRequest(item);
_Referer = result.Html;
sCookie = result.Cookie;
item = new HttpItem()
{
URL = "http://www.ems.com.cn/ems/rand?d=0.3501349513942344",
Method = "GET",
Cookie = sCookie,
ResultType = ResultType.Byte,
Referer = _Referer,
};
result = http.HttpRequest(item);
if (result.ResultByte != null)
{
//MessageBox.Show(result.ResultByte.Length.ToString());
MemoryStream ms = new MemoryStream(result.ResultByte);
ms.Seek(0, SeekOrigin.Current);
Bitmap bitmap = (Bitmap)Bitmap.FromStream(ms);
pictureBox1.Image = Image.FromStream(new MemoryStream(result.ResultByte));
#region OCR
UnCodebase ud = new UnCodebase(bitmap);
bitmap = ud.GrayByPixels();
ud.ClearNoise(128, 2);
pictureBox1.Image = bitmap;
tessnet2.Tesseract ocr = new tessnet2.Tesseract();//声明一个OCR类
ocr.SetVariable("tessedit_char_whitelist", "0123456789"); //设置识别变量,当前只能识别数字。
ocr.Init(Application.StartupPath + @"\\tmpe", "eng", true); //应用当前语言包。注,Tessnet2是支持多国语的。语言包下载链接:[url]http://code.google.com/p/tesseract-ocr/downloads/list[/url]
List<tessnet2.Word> OCRresult = ocr.DoOCR(bitmap, Rectangle.Empty);//执行识别操作
string code = OCRresult[0].Text;
#endregion
item = new HttpItem()
{
URL = "http://www.ems.com.cn/ems/order/singleQuery_t",
Method = "POST",
Postdata = string.Format("mailNum=BS025000854CN&checkCode={0}", code),
Cookie = sCookie,
Referer = _Referer,
};
result = http.HttpRequest(item);
textBox1.Text = code;
richTextBox1.Text = "读取成功 " + DateTime.Now;
richTextBox1.Text += "\r\n" + result.Html;
return bitmap;
}
else
{
richTextBox1.Text = result.Html;
}
return null;
//end try
|