【问题标题】:How to solve the HTTP 414 “Request URI too long” error in android / base64 image?如何解决android / base64图像中的HTTP 414“Request URI too long”错误?
【发布时间】:2016-01-25 10:37:57
【问题描述】:

我正在尝试将 base64 编码图像从我的应用程序提取到数据库(位于我的服务器中),但出现此错误。

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

我知道对于 URL,缩短它们可以修复错误,但我不能缩短 base64 字符串。

如何解决这个问题。

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");
        }
    }

【问题讨论】:

  • 您能否显示请求的完整代码?
  • 使用 PUT 或 POST 代替 GET。
  • 显然 HTTP GET 是向服务器发送数据的错误选择......如果您使用的是 POST,那么将数据作为请求的正文而不是作为 url 参数传递......

标签: android http


【解决方案1】:

尝试改造。在改造中使用 @FormUrlEncoded 和 @POST("ur_api_name") 并使用 @Field("data") 注释发送数据。

【讨论】:

  • 谢谢你的这个。这可以在@Query 出现 Http 414 URI 太长异常时使用
【解决方案2】:

在客户端和服务器端使用 POST 方法而不是 GET 方法

  • 在 Android 中通过 Retrofit
public interface TaskService {  

@FormUrlEncoded

@POST("tasks")

Call<String> createTask(@Field("title") String title);

}
  • 在服务器上的服务中,例如 WCF 服务
[ServiceContract]

public interface IOperationService
{

[OperationContract]

[WebInvoke(Method = "POST", UriTemplate = "tasks", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]

string createTask();

}
  • 那么您必须在 WCF Servic 的 Web.config 文件中设置 ma​​xReceivedMessageSize 参数
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="myServiceBinding" maxReceivedMessageSize="10485760" maxBufferSize="10485760" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00">
<security mode="None"/>
</binding>
</webHttpBinding>
</bindings>
</system.serviceModel>

【讨论】:

    【解决方案3】:

    首先,如果这是真的:

    我正在尝试将 base64 编码图像从我的应用程序提取到数据库(位于我的服务器中),但出现此错误。

    你应该改变

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

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

    从您的应用发送到数据库是上传,而不是下载。

    因为您正在上传,所以您应该使用HTTPGet 请求,而应使用HTTPPost 请求。不同之处在于 GET 用于下载,而 POST 用于上传。因此 POST 允许有正文。

    正文是消息的实际内容。现在,您似乎正在对请求中的图像进行编码,这不是一种好的做法。您已经遇到了它产生的问题。因为您使用的是正文,所以您将不得不更改您的服务器端应用程序。

    要发送正文,请查看how to make httpPost call with json encoded body?。这个问题专门关于使用 JSON 正文,但无论如何您都可以使用它。

    【讨论】:

      猜你喜欢
      • 2020-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      相关资源
      最近更新 更多