C# 将图像作为base64string发送到WebAPI;base64字符串太长
我最近学习了Ahsan Siddique的这些教程 使用Azure数据库在ASP.Net中开发RESTful API 第一部分 第二部分 第三部分 在Xamarin.Android中使用RESTful API 第四部分 我设法让所有的代码都正常工作,但我在尝试将base64字符串传递给web api的部分遇到了麻烦。教程中没有我遇到的问题。我在Postman上测试了我的PostAPI,得到了这个错误消息,HTTP错误414。请求URL太长 下面您可以看到我的部分代码:C# 将图像作为base64string发送到WebAPI;base64字符串太长,c#,xamarin.android,azure-sql-database,httpclient,restful-authentication,C#,Xamarin.android,Azure Sql Database,Httpclient,Restful Authentication,我最近学习了Ahsan Siddique的这些教程 使用Azure数据库在ASP.Net中开发RESTful API 第一部分 第二部分 第三部分 在Xamarin.Android中使用RESTful API 第四部分 我设法让所有的代码都正常工作,但我在尝试将base64字符串传递给web api的部分遇到了麻烦。教程中没有我遇到的问题。我在Postman上测试了我的PostAPI,得到了这个错误消息,HTTP错误414。请求URL太长 下面您可以看到我的部分代码: public St
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.");
}
如注释中所述,不要将base64图像作为url/GET参数的一部分发送 而是将其附加到POST请求的主体
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("profilepic", user.profilepic)
});
var result = await client.PostAsync(url, content);
为什么要将图像包括在URL中而不是文章正文中?base64编码的图像不应该是查询字符串/URL的一部分。请使用其他用户已经回复的post请求将其添加到正文中:您应该在此处使用post方法。不仅要克服数据太大而无法放入URI的问题,还要按照预期使用HTTP协议:GET用于读取操作,POST和其他方法(如PUT和DELETE)用于更新操作。你可能想看看嗨!谢谢你的快速回复,但我还是个新手。我想知道在添加了您的代码之后,我的控制器类应该是什么样子。我已经包括了上面的代码。对不起,我不明白你在找什么。建议的更改有效吗?如果这是与原始问题不同的问题,请标记为已接受,并打开与此问题相关的新问题。我正在尝试将base64string存储在Azure SQL数据库中。我目前正在开发一个Xamarin Android应用程序。然后请打开一个与您遇到的问题相关的新问题。如果这已经解决了原始帖子中概述的问题,请标记为已接受