ASP.NET Web API不允许使用冗长的base64 URI [英] ASP.NET Web API does not allow Lengthy base64 URI

查看:51
本文介绍了ASP.NET Web API不允许使用冗长的base64 URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Android客户端接收一个长的base64字符串,然后将其解码为Web API项目中的位图,以作为图像上传到Azure BLOB存储.但是,该项目返回此消息,并拒绝接受参数:

I'm trying to receive a lengthy base64 string from my Android Client and then decode it to a bitmap in my Web API Project to be uploaded as an image to an Azure BLOB Storage. However, the project returns this message and refuses to take in the parameters:

请求URL太长

HTTP错误414.请求URL太长.

HTTP Error 414. The request URL is too long.

显然base64字符串很长.我该如何解决并接受参数?我曾在堆栈中四处寻找类似的问题,但没有一个答案明确指出如何在Web API中对其进行修复.谢谢!

Obviously the base64 string is very long. How do I get around this and accept the parameters? I've looked around stack for a similar question but none of the answers clearly state ho to fix it in a Web API. Thank you!

这是我方法中的代码段:

Here's a code snippet from my method:

[HttpPost]
[Route("api/addnewpost")]
public IHttpActionResult AddNewMediaActivity(string base64String, string caption, string email, string type)
{
            byte[] f = Convert.FromBase64String(base64String);
            //more code........
}

我在Android上的客户代码:

HttpClient hc = new DefaultHttpClient();
String message;

HttpPost p = new HttpPost("http://mymobileaddress.azure-mobile.net/api/addactivity?base64String=" + image + "&caption=" + caption + "&email=" + email + "&type=" + type );
JSONObject object = new JSONObject();
try {
            object.put("base64String", image);
            object.put("email", image);
            object.put("base64String", image);

} catch (Exception ex) {

}

try {
           message = object.toString();

            p.setEntity(new StringEntity(message, "UTF8"));
            p.setHeader("Content-type", "application/json");
            p.setHeader("ACCEPT", "application/json");
            p.setHeader("X-ZUMO-APPLICATION", mobileServiceAppId);
            HttpResponse resp = hc.execute(p);
            if (resp != null) {
                if (resp.getStatusLine().getStatusCode() == 204)
                {

                }
            }

            Log.d("ER", "" + resp.getStatusLine().getStatusCode());
        } catch (Exception e) {
            e.printStackTrace();

        }

推荐答案

数据可以在请求的URL或正文中传输.您当前正在通过URL传输数据.而且,URL仅允许为有限长度.而是将您的base64字符串放在正文中.请参见参数绑定在ASP.NET Web API中.

Data can be either transmitted in the URL of a request, or in the body. You are currently transmitting the data in the URL. And the URL is only allowed to be a finite length. Instead, put your base64 string in the body. See Parameter Binding in ASP.NET Web API.

默认情况下,从URI中提取诸如字符串之类的简单参数.为了强制Web API在正文中寻找它,我们在参数中添加了 [FromBody] 属性.

By default, simple parameters like strings are pulled from the URI. To force Web API to look for it in the body, we add a [FromBody] attribute to the parameter.

[HttpPost]
[Route("api/addnewpost")]
public IHttpActionResult AddNewMediaActivity(string caption, string email,
    string type, [FromBody] string base64String)
{
    byte[] f = Convert.FromBase64String(base64String);
    //more code........
}

并更改您的Android客户端代码,以将base64字符串(而非URL)放入正文中.自从接触Java以来​​已经有一段时间了,我不知道您使用的是什么库,但是我想当您在构建HTTP请求时,可以选择将一些数据添加到Java的主体中.请求.

And change your Android client code to put the base64 string in the body, not the URL. It's been a while since I've touched Java and I don't know what library you're using, but I imagine when you're building your HTTP request that you'll have the option of adding some data to the body of the request.

这篇关于ASP.NET Web API不允许使用冗长的base64 URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆