【问题标题】:Sending image as base64string to WebAPI; base64 string is too long将图像作为 base64 字符串发送到 Web API; base64 字符串太长
【发布时间】:2019-04-11 21:37:19
【问题描述】:

我最近关注了 Ahsan Siddique 的这些教程

使用 Azure 数据库在 ASP.Net 中开发 RESTful API。

第 1 部分 https://www.c-sharpcorner.com/article/creating-sql-database-in-azure-portal/

第 2 部分 https://www.c-sharpcorner.com/article/developing-restful-api-in-asp-net-with-add-method/

第 3 部分 https://www.c-sharpcorner.com/article/developing-restful-apis-in-asp-net-with-retrieve-update-and-delete-functions/

在 Xamarin.Android 中使用 RESTful API

第 4 部分 https://www.c-sharpcorner.com/article/consuming-restful-apis-in-xamarin-android/

我设法让所有代码工作,但我被困在我试图将 base64 字符串传递给 web api 的部分。本教程没有我遇到的部分。我在 Postman 上测试了我的 POST API,我收到此错误消息“HTTP 错误 414。请求 URL 太长。”

您可以在下面看到我的部分代码:

public String BitmapToBase64(Bitmap bitmap)
{
    //Java.IO.ByteArrayOutputStream byteArrayOutputStream = new Java.IO.ByteArrayOutputStream();
    MemoryStream memStream = new MemoryStream();
    bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, memStream);
    byte[] byteArray = memStream.ToArray();
    return Base64.EncodeToString(byteArray, Base64Flags.Default);
}

User user = new User ();
user.ID = "1";
user.name = "Kelly";
user.profilepic = BitmapToBase64(NGetBitmap(uri)); //this is the part where base64string is too long

HttpClient client = new HttpClient();
string url = $"http://test.azurewebsites.net/api/User/{user.ID}?name={user.name}&profilepic={user.profilepic}";
var uri1 = new System.Uri(url); //base64
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response;
var json = JsonConvert.SerializeObject(feedback);
var content = new StringContent(json, Encoding.UTF8, "application/json");
response = await client.PostAsync(uri1, content);

if (response.StatusCode == System.Net.HttpStatusCode.Accepted)
{
    Toast.MakeText(this, "Your profile is updated.", ToastLength.Long).Show();
}
else
{
    Toast.MakeText(this, "Your profile is not updated." + feedback.profilepic, ToastLength.Long).Show();
}

我需要帮助!提前谢谢!

更新: 这就是我的控制器类目前的样子

public HttpResponseMessage Update_User(int ID, string name, string profilepic)
{
    if (!ModelState.IsValid)
    {
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
    }

    UserTable newUser = new UserTable();
    var entry = db.Entry<UserTable>(newUser);
    entry.Entity.ID = ID;
    entry.Entity.name = name;
    entry.Entity.profilepic = profilepic;
    entry.State = EntityState.Modified;

    try
    {
        db.SaveChanges();
    }
    catch (DbUpdateConcurrencyException ex)
    {
        return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
    }
    return Request.CreateResponse(HttpStatusCode.Accepted, "Your profile is updated.");
}

【问题讨论】:

  • 为什么在 URL 中而不是在 POST 正文中包含图像?
  • base64 编码图像不应成为查询字符串/URL 的一部分。请使用发布请求将其添加到正文中
  • 正如其他用户已经回复的:您应该在这里使用 POST 方法。不仅要克服数据太大而无法放入 URI 的问题,还要按预期使用 HTTP 协议:GET 用于读取操作,POST(以及其他方法,如 PUT 和 DELETE)用于更新操作。你可能想看看w3schools.com/tags/ref_httpmethods.asp

标签: c# xamarin.android azure-sql-database httpclient restful-authentication


【解决方案1】:

如 cmets 中所述,请勿将 base64 图像作为 url/GET 参数的一部分发送。

而是将其附加到 POST 请求的正文中。

var content = new FormUrlEncodedContent(new[]
{
    new KeyValuePair<string, string>("profilepic", user.profilepic)
});
var result = await client.PostAsync(url, content);

【讨论】:

  • 嗨!感谢您的快速回复,但因为我还是一个新手。我想知道添加代码后我的控制器类应该是什么样子。我已经包含了上面的代码。
  • 对不起,我不明白你在追求什么。建议的更改是否有效?如果这是与原始问题不同的问题,请标记为已接受并打开与该问题相关的新问题。
  • 我正在尝试将 base64string 存储在 Azure SQL 数据库中。我目前正在开发 Xamarin Android 应用程序。
  • 那么请打开一个与您遇到的问题相关的新问题。如果这已经解决了原帖中列出的问题,请标记为已接受
猜你喜欢
  • 2015-08-20
  • 2017-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-28
  • 1970-01-01
  • 2019-01-27
  • 1970-01-01
相关资源
最近更新 更多