阿里介面呼叫——智慧植物識別(含花卉與雜草)
阿新 • • 發佈:2019-02-07
方法一,根據RestClient,輕量級元件,在GitHub的地址
public object PsotBaiDuAPIIdentify() { var client = new RestClient("http://plantgw.nongbangzhu.cn/plant/recognize2"); var request = new RestRequest(Method.POST); request.AddHeader("postman-token", "ae788751-d4f8-8a70-4073-865affb1c509"); request.AddHeader("cache-control", "no-cache"); request.AddHeader("authorization", "APPCODE 你的code");//appcode和code之間有個空格 request.AddHeader("content-type", "application/x-www-form-urlencoded"); request.AddParameter("application/x-www-form-urlencoded", "img_base64=圖片轉為string", ParameterType.RequestBody); var response = client.Execute(request); return base.Json(new { status = 1, success = true, msg = "", list = response }); }
方法二,根據WebRequest 來實現
private const String host = "http://plantgw.nongbangzhu.cn"; private const String path = "/plant/recognize2"; private const String method = "POST"; private const String appcode = "你的code碼"; public object PsotaliAPIIdentify2() { var bodys = "img_base64=";//img_base64 var url = host + path; HttpPostedFileBase files = Request.Files[0] ?? null; if (files == null || files.ContentLength <= 0 || Request.Files.Count != 1) return base.Json(new { status = -2, success = false, msg = "引數錯誤,請核實", list = "" }); try { var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url); request.Method = method; request.Accept = "application/json;"; request.Headers.Add("cache-control", "no-cache"); //獲取許可權code request.Headers.Add("authorization", "APPCODE " + appcode); request.ContentType = "application/x-www-form-urlencoded"; //轉換img為string byte[] buffer = StreamToBytes(files.InputStream); string imgstr = Convert.ToBase64String(buffer); var base64Img = System.Web.HttpUtility.UrlEncode(imgstr); //上傳 byte[] data = Encoding.UTF8.GetBytes(bodys + base64Img); request.ContentLength = data.Length; request.GetRequestStream().Write(data, 0, data.Length); //獲取返回值 var response = (System.Net.HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); //獲取返回值 var reastr = reader.ReadToEnd(); Log.Debug(reastr); return base.Json(new { status = 1, success = true, msg = "", list = reastr }); } catch (Exception err) { Log.Debug(err.Message); return base.Json(new { status = -1, success = false, msg = err.Message, list = "" }); } }
返回結果:
{ "Status": 0, "Message": "OK", "Result": [ { "Score": "99.74", "AliasList": [], "Genus": "麒麟尾屬", "InfoCode": "5yj9UbZgitGE9MhO", "AliasName": "", "Family": "天南星科", "ImageUrl": "https://static.nongbangzhu.cn/samples_v4/p11k/p11k-watermark/%E7%BB%BF%E8%90%9D/1199806cbf1d6f78.jpg", "LatinName": "Epipremnum aureum", "Name": "綠蘿" }, { "Score": "0.05", "AliasList": [], "Genus": "廣東萬年青屬", "InfoCode": "CIWLogDlplSmcgvK", "AliasName": "", "Family": "天南星科", "ImageUrl": "https://static.nongbangzhu.cn/samples_v4/p11k/p11k-watermark/%E5%B9%BF%E4%B8%9C%E4%B8%87%E5%B9%B4%E9%9D%92/1048b2b0c7f3793c.jpg", "LatinName": "Aglaonema modestum", "Name": "廣東萬年青" }, { "Score": "0.05", "AliasList": [ "烏楊", "茄冬樹", "紅桐" ], "Genus": "秋楓屬", "InfoCode": "xrKtFq7PdQZ6qYAZ", "AliasName": "烏楊、茄冬樹、紅桐", "Family": "葉下珠科", "ImageUrl": "https://static.nongbangzhu.cn/samples_v4/p11k/p11k-watermark/%E9%87%8D%E9%98%B3%E6%9C%A8/10029e17ff255515.jpg", "LatinName": "Bischofia polycarpa", "Name": "重陽木" }, { "Score": "0.02", "AliasList": [ "老虎芋", "大麻芋", "不拱" ], "Genus": "海芋屬", "InfoCode": "1sedktIatQiJcOwc", "AliasName": "老虎芋、大麻芋、不拱", "Family": "天南星科", "ImageUrl": "https://static.nongbangzhu.cn/samples_v4/p11k/p11k-watermark/%E5%B0%96%E5%B0%BE%E8%8A%8B/104cae5d2c6736e5.jpg", "LatinName": "Alocasia cucullata", "Name": "尖尾芋" }, { "Score": "0.01", "AliasList": [ "箭葉芋", "綠精靈", "白斑葉" ], "Genus": "合果芋屬", "InfoCode": "9WRpxCpa7JTzjamH", "AliasName": "箭葉芋、綠精靈、白斑葉", "Family": "天南星科", "ImageUrl": "https://static.nongbangzhu.cn/samples_v4/p11k/p11k-watermark/%E5%90%88%E6%9E%9C%E8%8A%8B/12587fe7732e8a8a.jpg", "LatinName": "Syngonium podophyllum", "Name": "合果芋" } ] }