private const String host = "https://dm-58.data.aliyun.com"; private const String path = "/rest/160601/ocr/ocr_business_license.json"; private const String method = "POST"; private const String appcode = "本身的AppCode"; public static void Main(string[] args) { String querys = ""; String img_file = "圖片路徑"; FileStream fs = new FileStream(img_file, FileMode.Open); BinaryReader br = new BinaryReader(fs); byte[] contentBytes = br.ReadBytes(Convert.ToInt32(fs.Length)); String base64 = System.Convert.ToBase64String(contentBytes); String bodys = "{\"image\":\"" + base64 + "\""; //對圖片內容進行Base64編碼 String url = host + path; HttpWebRequest httpRequest = null; HttpWebResponse httpResponse = null; if (0 < querys.Length) { url = url + "?" + querys; } if (host.Contains("https://")) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url)); } else { httpRequest = (HttpWebRequest)WebRequest.Create(url); } httpRequest.Method = method; httpRequest.Headers.Add("Authorization", "APPCODE " + appcode); //根據API的要求,定義相對應的Content-Type httpRequest.ContentType = "application/json; charset=UTF-8"; // 設置並解析 格式 String config = "{\\\"side\\\":\\\"face\\\"}"; bodys = "{\"image\":\"" + base64 + "\""; if (config.Length > 0) { bodys += ",\"configure\" :\"" + config + "\""; } bodys += "}"; if (0 < bodys.Length) { byte[] data = Encoding.UTF8.GetBytes(bodys); using (Stream stream = httpRequest.GetRequestStream()) { stream.Write(data, 0, data.Length); } } try { httpResponse = (HttpWebResponse)httpRequest.GetResponse(); } catch (WebException ex) { httpResponse = (HttpWebResponse)ex.Response; } Console.WriteLine(httpResponse.StatusCode); Console.WriteLine(httpResponse.Method); Console.WriteLine(httpResponse.Headers); Stream st = httpResponse.GetResponseStream(); StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8")); Console.WriteLine(reader.ReadToEnd()); Console.WriteLine("\n"); Console.ReadLine(); } public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; }