利用百度接口,识别身份证

http://apistore.baidu.com/apiworks/servicedetail/146.html


protected void Button1_Click(object sender, EventArgs e) { Stream fstream = FileUpload1.PostedFile.InputStream; byte[] img = new byte[fstream.Length]; fstream.Read(img, 0, img.Length); fstream.Close(); string imgdata = Convert.ToBase64String(img); string url = "http://apis.baidu.com/apistore/idlocr/ocr"; string param = "fromdevice=pc&clientip=10.10.10.0&detecttype=LocateRecognize&languagetype=CHN_ENG&imagetype=1&image="+imgdata; string result = request(url,param); ss.Text = result; } /// <summary> /// 发送HTTP请求 /// </summary> /// <param name="url">请求的URL</param> /// <param name="param">请求的参数</param> /// <returns>请求结果</returns> public static string request(string url, string param) { string strURL = url; System.Net.HttpWebRequest request; request = (System.Net.HttpWebRequest)WebRequest.Create(strURL); request.Method = "POST"; // 添加header request.Headers.Add("apikey", "e0bbb1655505bcb36a6581a5b632207a"); request.ContentType = "application/x-www-form-urlencoded"; string paraUrlCoded = param; byte[] payload; payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); request.ContentLength = payload.Length; Stream writer = request.GetRequestStream(); writer.Write(payload, 0, payload.Length); writer.Close(); System.Net.HttpWebResponse response; response = (System.Net.HttpWebResponse)request.GetResponse(); System.IO.Stream s; s = response.GetResponseStream(); string StrDate = ""; string strValue = ""; StreamReader Reader = new StreamReader(s, Encoding.UTF8); while ((StrDate = Reader.ReadLine()) != null) { strValue += StrDate + "<br>"; } return strValue; }

  

原文地址:https://www.cnblogs.com/mqingqing123/p/4901487.html