ASP.NET Web API does not allow Lengthy base64 URI

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:

Request URL Too Long

HTTP Error 414. The request URL is too long.

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........
}

My Client code on 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();

        }

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.

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........
}

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.


(I have deleted the other one, I want to keep this one because the issue was using asp.net 5 beta 8 and is more recent)

I had this issue recently and the parameter was moved to receive a byte[] (byte array) From Body

First I tried from body and the string was received but the string was truncated and the file can't be recreated in the server side.

In my application the base64 came from a conversion of an array buffer created from an input file.

The base64 is sent in a json from angular.

链接地址: http://www.djcxy.com/p/45852.html

上一篇: 如何用java发送curl

下一篇: ASP.NET Web API不允许使用冗长的base64 URI