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;
}