How to solve the HTTP 414 “Request URI too long” e

2019-05-31 04:13发布

I am trying to pull an base64 encoded image from my app to database(which is in my server), but I get this error.

the server responded with a status of 414 (Request-URI Too Long)

I know that for URLs, shortening them fixes the error but I can't shorten a base64 string.

How to fix this one.

HttpGet httpGet = new HttpGet(URL);

try {

 HttpResponse response = httpClient.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream inputStream = entity.getContent();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(inputStream));
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
            inputStream.close();
        } else {
            Log.d("JSON", "Failed to download file");
        }
    }

标签: android http
2条回答
该账号已被封号
2楼-- · 2019-05-31 04:49

First off, if this is true:

I am trying to pull an base64 encoded image from my app to database(which is in my server), but I get this error.

you should change

Log.d("JSON", "Failed to download file");

to

Log.d("JSON", "Failed to upload file");

Sending from your app to the database is uploading, not downloading.

Because you're uploading, you should not use a HTTPGet request, but a HTTPPost request. The difference is that GET is made for downloading, while POST is made for uploading. Therefore POST is allowed to have a body.

A body is the actual content of the message. Right now it seems like you're encoding the image in the request, which is not considered good practice. You already ran into the problems it creates. Because you're using a body you will have to change your server-side application.

For sending a body, take a look at how to make httpPost call with json encoded body?. This question is specifically about using a JSON body, but you could use it anyways.

查看更多
SAY GOODBYE
3楼-- · 2019-05-31 04:51

Try Retrofit. Use @FormUrlEncoded in retrofit and @POST("ur_api_name") and send the data using @Field("data") annotation.

查看更多
登录 后发表回答