美文网首页
[C#] 调用腾讯云接口 名片识别(v2)

[C#] 调用腾讯云接口 名片识别(v2)

作者: 贪吃鱼 | 来源:发表于2018-11-30 10:38 被阅读88次
            public ActionResult GetImgInfo(HttpPostedFileBase image)
            {
    //            var stream = image.InputStream;
    //            var bytes = new byte[stream.Length];
    //            stream.Read(bytes, 0, bytes.Length);
    //            // 设置当前流的位置为流的开始 
    //            stream.Seek(0, SeekOrigin.Begin);
                var filename = image.FileName;
                var bytes = ImgHelper.GetPicThumbnail(image.InputStream, 50);
                var resstr = string.Empty;
                try
                {
                    var webReq = (HttpWebRequest)WebRequest.Create("http://recognition.image.myqcloud.com/ocr/businesscard");
                    webReq.Method = "POST";
                    var boundary = "----" + DateTime.Now.Ticks.ToString("x");
                    var formdataTemplate = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n";
                    var dataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
                    var formdata = string.Format(formdataTemplate, "image", filename);
                    webReq.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
                    webReq.Host = "recognition.image.myqcloud.com";
                    webReq.Headers.Add("authorization", GetSign());
                    //文件头
                    var formdataByte = Encoding.ASCII.GetBytes(formdata);
                    //尾
                    var footer = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
                    Stream requestStream = webReq.GetRequestStream();
                    requestStream.Write(formdataByte, 0, formdataByte.Length);//写入 文件头
                    requestStream.Write(bytes, 0, bytes.Length);//写入 文件
                    var dataByte1 = Encoding.ASCII.GetBytes(string.Format(dataTemplate, "appid", "xxxxxx"));
                    requestStream.Write(dataByte1, 0, dataByte1.Length);//写入 参数1
                    //var dataByte2 = Encoding.ASCII.GetBytes(string.Format(dataTemplate, "tid", 48650));
                    //requestStream.Write(dataByte2, 0, dataByte2.Length);//写入 参数2
                    //var dataByte3 = Encoding.ASCII.GetBytes(string.Format(dataTemplate, "uid", 48650));
                    //requestStream.Write(dataByte3, 0, dataByte3.Length);//写入 参数3
                    //var dataByte4 = Encoding.ASCII.GetBytes(string.Format(dataTemplate, "Filename", name));
                    //requestStream.Write(dataByte4, 0, dataByte4.Length);//写入 参数4
                    //var dataByte5 = Encoding.ASCII.GetBytes(string.Format(dataTemplate, "Upload", "Submit Query"));
                    //requestStream.Write(dataByte5, 0, dataByte5.Length);//写入 参数5
                    requestStream.Write(footer, 0, footer.Length);//写入 尾
                    requestStream.Close();
                    var response = (HttpWebResponse)webReq.GetResponse();
                    var sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    var res = sr.ReadToEnd();
                    sr.Close();
                    response.Close();
                    resstr = res;
                }
                catch (WebException we)
                {
                    if (we.Status != WebExceptionStatus.ProtocolError) return Json(resstr);
                    using (var s = we.Response.GetResponseStream())
                    {
                        var reader = new StreamReader(s, Encoding.UTF8);
                        resstr = reader.ReadToEnd();
                    }
                }
                return Json(resstr);
            }
    

    上面就是调用腾讯云的接口 利用 C# 后台模拟form提交请求

    把appid改成自己的腾讯云的id就可以用了

    我在网上没有搜到 C#的sdk 然后自己尝试了后台提交就是不好使, 后来找到模拟form表单提交的方法好使了, 主要就是提交的格式问题

    此方法转载https://blog.csdn.net/sh2015yao/article/details/39317299

    相关文章

      网友评论

          本文标题:[C#] 调用腾讯云接口 名片识别(v2)

          本文链接:https://www.haomeiwen.com/subject/dwmccqtx.html