将图像作为base64string发送到WebAPI; base64字符串太长

时间:2018-11-08 11:19:53

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

我最近遵循了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.");
}

1 个答案:

答案 0 :(得分:0)

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

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

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