发送Base64字符串到c# MVC Api得到错误

本文关键字:Api 错误 MVC Base64 字符串 发送 | 更新日期: 2023-09-27 18:01:50

我用下面这行把[]字节转换成64进制字符串

 filebase64 = Convert.ToBase64String(attachmentcontetn.Content);

,我把filebase64放在json中,像这样:

 "{'RequestId':'" + requestforminst.Id
                                                     + "','filebase64':'" + filebase64 + "'}"
jsoninput是RestApi的InputParameter,它包含Base64数据和一些额外的数据
  string webapiUrl = generalurl;
        using (var client = new WebClient())
        {
            client.Encoding = Encoding.UTF8;
            client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            string logineusername = username;
            string logineduserpassword = password;
            client.Credentials = new NetworkCredential(logineusername, logineduserpassword);
            var result = client.UploadString(webapiUrl, "POST", "=" + jsoninput);
            return result;

和在REST Api中,我将Base 64转换为[]字节,用这一行:

Convert.FromBase64String(modeldata.filebase64)

,但它得到错误:"无效的长度为Base-64字符数组或字符串。"在调用restapi之前,我检查了我的base64数据并将其转换为[]字节,它工作正确,在目标服务器得到错误

发送Base64字符串到c# MVC Api得到错误

你不能在webapi上发布字符串,也许你应该定义一个类或将接收类型修改为动态

也许你应该使用HttpServerUtility。UrlEncode在post

之前编码base64字符串