方法一,根据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": "合果芋"
}
]
}