private static void doPutStream() { String path = "/putstream"; Dictionary <String, String> headers = new Dictionary <string, string>(); Dictionary <String, String> querys = new Dictionary <string, string>(); Dictionary <String, String> bodys = new Dictionary <string, string>(); List <String> signHeader = new List <String>(); byte[] bobyContent = new byte[10]; //设定Content-Type,根据服务器端接受的值来设置 headers.Add(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_STREAM); //设定Accept,根据服务器端接受的值来设置 headers.Add(HttpHeader.HTTP_HEADER_ACCEPT, ContentType.CONTENT_TYPE_JSON); //注意:如果有非Form形式数据(body中只有value,没有key);如果body中是key/value形式数据,不要指定此行 headers.Add(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.Base64AndMD5(bobyContent)); //如果是调用测试环境请设置 //headers.Add(SystemHeader.X_CA_STAGE, "TEST"); //注意:业务header部分,如果没有则无此行(如果有中文,请做Utf8ToIso88591处理) headers.Add("b-header2", MessageDigestUtil.Utf8ToIso88591("headervalue1")); headers.Add("a-header1", MessageDigestUtil.Utf8ToIso88591("headervalue2处理")); //注意:业务query部分,如果没有则无此行;请不要、不要、不要做UrlEncode处理 querys.Add("b-query2", "queryvalue2"); querys.Add("a-query1", "queryvalue1"); //注意:业务body部分 bodys.Add("", BitConverter.ToString(bobyContent)); //指定参与签名的header signHeader.Add(SystemHeader.X_CA_TIMESTAMP); signHeader.Add("a-header1"); signHeader.Add("b-header2"); using (HttpWebResponse response = HttpUtil.HttpPut(host, path, appKey, appSecret, 30000, headers, querys, bodys, signHeader)) { Console.WriteLine(response.StatusCode); Console.WriteLine(response.Method); Console.WriteLine(response.Headers); Stream st = response.GetResponseStream(); StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8")); Console.WriteLine(reader.ReadToEnd()); Console.WriteLine(Constants.LF); } }
private static void doPostString() { String bobyContent = "[{'time':'2019-05-22 21:45:00','unique_id':'ca2dc621789e4588a7e1d78eb5837537','floor_id':'636300233436576132','floor_name':'B1','vistor_num':5,'leaving_num':38,'position':'BeijingPuP_214_B1F_出口','mall_area':'21401'},{'time':'2019-05-22 22:00:00','unique_id':'0f898431fdc749e1b6ba91626614fd7f','floor_id':'636300233436576132','floor_name':'B1','vistor_num':3,'leaving_num':3,'position':'BeijingPuP_214_B1F_出口','mall_area':'21401'}]"; String path = "/sync_data/sync_with_json"; Dictionary <String, String> headers = new Dictionary <string, string>(); Dictionary <String, String> querys = new Dictionary <string, string>(); Dictionary <String, String> bodys = new Dictionary <string, string>(); List <String> signHeader = new List <String>(); //设定Content-Type,根据服务器端接受的值来设置 headers.Add(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_JSON); //设定Accept,根据服务器端接受的值来设置 headers.Add(HttpHeader.HTTP_HEADER_ACCEPT, ContentType.CONTENT_TYPE_JSON); //注意:如果有非Form形式数据(body中只有value,没有key);如果body中是key/value形式数据,不要指定此行 headers.Add(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.Base64AndMD5(Encoding.UTF8.GetBytes(bobyContent))); //如果是调用测试环境请设置 //headers.Add(SystemHeader.X_CA_STAGE, "TEST"); //注意:业务header部分,如果没有则无此行(如果有中文,请做Utf8ToIso88591处理) headers.Add("b-header2", MessageDigestUtil.Utf8ToIso88591("headervalue1")); headers.Add("a-header1", MessageDigestUtil.Utf8ToIso88591("headervalue2处理")); //注意:业务query部分,如果没有则无此行;请不要、不要、不要做UrlEncode处理 querys.Add("b-query2", "queryvalue2"); querys.Add("a-query1", "queryvalue1"); //注意:业务body部分 bodys.Add("", bobyContent); //指定参与签名的header signHeader.Add(SystemHeader.X_CA_TIMESTAMP); signHeader.Add("a-header1"); signHeader.Add("b-header2"); using (HttpWebResponse response = HttpUtil.HttpPost(host, path, appKey, appSecret, 30000, headers, querys, bodys, signHeader)) { Console.WriteLine(response.StatusCode); Console.WriteLine(response.Method); Console.WriteLine(response.Headers); Stream st = response.GetResponseStream(); StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8")); Console.WriteLine(reader.ReadToEnd()); Console.WriteLine(Constants.LF); } }
private static void doPostStream() { String url = "/poststream"; Dictionary <String, String> headers = new Dictionary <string, string>(); headers.Add(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_STREAM); headers.Add(HttpHeader.HTTP_HEADER_ACCEPT, ContentType.CONTENT_TYPE_JSON); byte[] bytesBody = Encoding.UTF8.GetBytes("post bytes body content".ToCharArray()); headers.Add(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.Base64AndMD5(bytesBody)); using (HttpWebResponse response = HttpUtil.HttpPost(host + url, appKey, appSecret, 30000, headers, bytesBody, null)) { Console.WriteLine(response.StatusCode); Console.WriteLine(response.Method); Console.WriteLine(response.Headers); Stream st = response.GetResponseStream(); StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8")); Console.WriteLine(reader.ReadToEnd()); Console.WriteLine(Constants.LF); } }
private static void doPostString() { String bobyContent = "{\"qs\": [\"nice to meet you.\", \"hello world\"], \"source\": \"en\", \"target\": \"zh\", \"domain\":\"medical\"}"; String path = "/translate_batch_v2"; Dictionary <String, String> headers = new Dictionary <string, string>(); Dictionary <String, String> querys = new Dictionary <string, string>(); Dictionary <String, String> bodys = new Dictionary <string, string>(); List <String> signHeader = new List <String>(); //设定Content-Type,根据服务器端接受的值来设置 headers.Add(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_JSON); //设定Accept,根据服务器端接受的值来设置 headers.Add(HttpHeader.HTTP_HEADER_ACCEPT, ContentType.CONTENT_TYPE_JSON); //注意:如果有非Form形式数据(body中只有value,没有key);如果body中是key/value形式数据,不要指定此行 headers.Add(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.Base64AndMD5(Encoding.UTF8.GetBytes(bobyContent))); //如果是调用测试环境请设置 //headers.Add(SystemHeader.X_CA_STAGE, "TEST"); //注意:业务body部分 bodys.Add("", bobyContent); //指定参与签名的header signHeader.Add(SystemHeader.X_CA_TIMESTAMP); using (HttpWebResponse response = HttpUtil.HttpPost(host, path, appKey, appSecret, 30000, headers, querys, bodys, signHeader)) { Console.WriteLine(response.StatusCode); Console.WriteLine(response.Method); Console.WriteLine(response.Headers); Stream st = response.GetResponseStream(); StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8")); Console.WriteLine(reader.ReadToEnd()); Console.WriteLine(Constants.LF); } }
static void Main(string[] args) { String url = "http://dm-51.data.aliyun.com/rest/160601/ocr/ocr_idcard.json"; String appKey = "你自己的AppKey"; String appSecret = "你自己的AppSecret"; String imgFile = "图片路径"; //如果没有configure字段,configure设为'' //String configure = ''; String configure = "{\\\"side\\\":\\\"face\\\"}"; FileStream fs = new FileStream(imgFile, FileMode.Open); BinaryReader br = new BinaryReader(fs); byte[] contentBytes = br.ReadBytes(Convert.ToInt32(fs.Length)); String base64 = System.Convert.ToBase64String(contentBytes); String bodys; bodys = "{\"image\":\"" + base64 + "\""; if (configure.Length > 0) { bodys += ",\"configure\" :\"" + configure + "\""; } bodys += "}"; Dictionary <String, String> headers = new Dictionary <string, string>(); Dictionary <String, String> querys = new Dictionary <string, string>(); Dictionary <String, String> bodys_map = new Dictionary <string, string>(); List <String> signHeader = new List <String>(); //设定Content-Type,根据服务器端接受的值来设置 headers.Add(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_JSON); //设定Accept,根据服务器端接受的值来设置 headers.Add(HttpHeader.HTTP_HEADER_ACCEPT, ContentType.CONTENT_TYPE_JSON); //注意:如果有非Form形式数据(body中只有value,没有key);如果body中是key/value形式数据,不要指定此行 headers.Add(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.Base64AndMD5(Encoding.UTF8.GetBytes(bodys))); //注意:业务body部分 bodys_map.Add("", bodys); //指定参与签名的header signHeader.Add(SystemHeader.X_CA_TIMESTAMP); Uri myUri = new Uri(url); using (HttpWebResponse response = HttpUtil.HttpPost(myUri.Scheme + "://" + myUri.Host, myUri.AbsolutePath, appKey, appSecret, 30000, headers, querys, bodys_map, signHeader)) { if (response.StatusCode != HttpStatusCode.OK) { Console.WriteLine("http error code: " + response.StatusCode); Console.WriteLine("error in header: " + response.GetResponseHeader("X-Ca-Error-Message")); Console.WriteLine("error in body: "); Stream st = response.GetResponseStream(); StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8")); Console.WriteLine(reader.ReadToEnd()); } else { Stream st = response.GetResponseStream(); StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8")); Console.WriteLine(reader.ReadToEnd()); Console.WriteLine(Constants.LF); } } }