using Newtonsoft.Json.Linq; using qingdaoport; using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Web; namespace DSWeb.MvcShipping.DAL.MsOpSeaeYardDAL { /// /// 大亚场站 /// public class DataMining { string _TimeTemp = ""; HttpHelper http = new HttpHelper(); private string _COOKIES = ""; public string GetHTMLWithMBLNO(string mblno) { string str = getVerificationCode(); string html = GetWHLData(mblno,str); return html; } int trytimes = 0; public string GetWHLData(string mblno, string code) { string timeTemp = ConvertDateTimeInt(DateTime.Now).ToString(); ; string whlURL = "http://www.yydy.com/dyxt/BasicService/QueryService.aspx?" + timeTemp + "&oper=QueryByBill¶m=" + mblno + "&CheckCode=" + code; WebRequest request = WebRequest.Create(whlURL); request.Method = "GET"; request.Headers.Add("Cookie", _COOKIES); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string url = response.ResponseUri.AbsoluteUri; StreamReader reader = new StreamReader(response.GetResponseStream()); string content = reader.ReadToEnd(); if (content == "OK") { trytimes = 0; return Get2ndRequest(mblno); } else if (content== "CENABLE") { if (trytimes>3) { return "error try " + trytimes + " times"; } else { trytimes++; return GetWHLData(mblno, code); } } else { return "错误:场站请求失败,请稍后重试!"; } } public string Get2ndRequest(string mblno) { string timeTemp = ConvertDateTimeInt(DateTime.Now).ToString(); ; string whlURL = "http://www.yydy.com/dyxt/Query/ExportByBill.aspx?s_no=" + mblno; WebRequest request = WebRequest.Create(whlURL); request.Method = "GET"; request.Headers.Add("Cookie", _COOKIES); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string url = response.ResponseUri.AbsoluteUri; StreamReader reader = new StreamReader(response.GetResponseStream()); string content = reader.ReadToEnd(); return content; } public string getVerificationCode() { _TimeTemp = ConvertDateTimeInt(DateTime.Now).ToString(); HttpItem item = new HttpItem() { URL = "http://www.yydy.com/dyxt/BasicService/CreateCheckCode.aspx?" + _TimeTemp, Method = "get",//URL 可选项 默认为Get ResultType = ResultType.Byte }; HttpResult result = http.GetHtml(item); Bitmap bitmap = BytesToBitmap(result.ResultByte); bitmap = unNoise(bitmap, 0); Bitmap bm2 = new Bitmap(bitmap.Width, bitmap.Height); Graphics g = Graphics.FromImage(bm2); g.DrawImageUnscaled(bitmap, 0, 0); string guid = Guid.NewGuid().ToString(); string path = HttpRuntime.AppDomainAppPath.ToString() + "qdporttemp\\"; string filename = path + guid + ".png"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } bm2.Save(filename); //调用百度云文字识别 OcrDemo ocr = new OcrDemo(); JToken o = ocr.GeneralBasic(filename); File.Delete(filename); var errorMsg = o.Value("error_msg"); var wordsNum = o.Value("words_result_num"); if (String.IsNullOrEmpty(errorMsg)) { if (wordsNum > 0) { var txts = from p in (JArray)o.Root["words_result"] select (string)p["words"]; string words = txts.First(); _COOKIES = result.Cookie; return words; } return getVerificationCode(); } return errorMsg; } #region 图像处理部分 public static long ConvertDateTimeInt(System.DateTime time) { //double intResult = 0; System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0)); //intResult = (time- startTime).TotalMilliseconds; long t = (time.Ticks - startTime.Ticks) / 10000; //除10000调整为13位 return t; } public static Bitmap BytesToBitmap(byte[] Bytes) { MemoryStream stream = null; try { stream = new MemoryStream(Bytes); return new Bitmap((Image)new Bitmap(stream)); } catch (ArgumentNullException ex) { throw ex; } catch (ArgumentException ex) { throw ex; } finally { stream.Close(); } } private Bitmap unNoise(Bitmap bmpOut, int type) { UnCodebase imageNoise = new UnCodebase(bmpOut); //灰度处理 bmpOut = imageNoise.ToGray(); imageNoise = new UnCodebase(bmpOut); //得到阈值 int grayvalue = imageNoise.GetDgGrayValue(); //降噪 if (type == 0) bmpOut = imageNoise.ClearNoise(grayvalue); else bmpOut = imageNoise.ClearNoise(grayvalue, 3); imageNoise = new UnCodebase(bmpOut); bmpOut = imageNoise.ConvertTo1Bpp1(); imageNoise = new UnCodebase(bmpOut); bmpOut = imageNoise.Sharpen(1); return bmpOut; } #endregion } }