代码拉取完成,页面将自动刷新
特别说明:如果 Issue 报告为问题且开发成员回复确认问题之后但三天内都不能得到反馈,则视为无效Issue。
2.9.3
远程请求设置 Body 参数,后台服务一直返回识别不了参数
上面红色框里面的代码正常,下面用咱furion请求一直识别不了参数image,下面是服务接口说明
无法测试,issue无效,没时间去搭建环境,获取token参数等,麻烦提供测试代码,异常代码,完整错误堆栈
public async Task UploadFile(IFormFile file)
{
using var fileStream = file.OpenReadStream();
byte[] imageBytes = new byte[file.Length];
fileStream.Read(imageBytes, 0, (int)file.Length);
var imageBase64 = HttpUtility.UrlEncode(Convert.ToBase64String(imageBytes));
string token = "24.af39d5d2cae26aa197a55be6ee93049c.2592000.1626768578.282335-24400792";
string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/taxi_receipt?access_token=" + token;
// 正常
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
request.Method = "post";
request.KeepAlive = true;
var str = "image=" + imageBase64; // 参数用“=”连接
byte[] buffer = Encoding.Default.GetBytes(str);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
var response = (HttpWebResponse)request.GetResponse();
var reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
var res = reader.ReadToEnd();
Console.WriteLine(res);
// 不正常
var res2 = await host.SetBody(new { image = imageBase64 }).GetAsStringAsync();
Console.WriteLine(res2);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1、你犯了非常低级的错误,你竟然带 Body
参数用了 Get
请求!!!!!!!!
2、.NET Core 传递 application/x-www-form-urlencoded
参数无需自己 HttpUtility.UrlEncode
编码!!
正确应该是:
public async Task UploadFile(IFormFile file)
{
using var fileStream = file.OpenReadStream();
byte[] imageBytes = new byte[file.Length];
fileStream.Read(imageBytes, 0, (int)file.Length);
// 这里不需要编码!!!!!application/x-www-form-urlencoded 自动编码!!!
//var imageBase64 = Uri.EscapeDataString(Convert.ToBase64String(imageBytes));
string token = "24.af39d5d2cae26aa197a55be6ee93049c.2592000.1626768578.282335-24400792";
string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/taxi_receipt?access_token=" + token;
var res2 = await host.SetBody(new { image = Convert.ToBase64String(imageBytes) }, "application/x-www-form-urlencoded").PostAsStringAsync();
Console.WriteLine(res2);
}
多谢 我的理解问题,我还显示的设置请求方式SetHttpMethod
登录 后才可以发表评论