Post BASE64 到服务器

Post BASE64 to server

我正在尝试将一张带有 BASE64 的图像发送到服务器,但总是收到 408 消息 "The request body did not contain the specified number of bytes. Got 13.140, expected 88.461"。我该如何解决?

我尝试使用 Retrofit、HttpURLConnection 并得到了同样的错误。我认为这是应用程序的一些参数。

 Gson gson = new Gson();
                    String jsonParam = gson.toJson(enviarPlataforma);

                    URL url = new URL("");
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("POST");

                    conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
                    conn.setRequestProperty("Accept", "application/json");
                    conn.setDoOutput(true);
                    conn.setDoInput(true);
                    conn.setFixedLengthStreamingMode(jsonParam.getBytes().length);


                    Log.i("JSON", jsonParam.toString());
                    DataOutputStream os = new DataOutputStream(conn.getOutputStream());
                    os.writeBytes(jsonParam);

                    os.flush();
                    os.close();

                    Log.i("STATUS", String.valueOf(conn.getResponseCode()));
                    Log.i("MSG", conn.getResponseMessage());

                    conn.disconnect();
                } catch (Exception e) {
                    e.printStackTrace();
                }

必须采用 UTF-8 字节,并按原样发送。 DataOutputStream 完全用于其他用途。因为它是一次写入,所以您可以简单地使用从连接中获得的 OutputStream。 BufferedOutputStream 可能没有任何用处。

永远不需要关闭前刷新。只需在站立的连续线上冲洗即可。

Try-with-resources 会自动关闭,也会在抛出某些异常时关闭。

                conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
                conn.setRequestProperty("Accept", "application/json");
                conn.setDoOutput(true);
                //conn.setDoInput(true);

                byte[] content = jsonParam.getBytes("UTF-8"):
                conn.setFixedLengthStreamingMode(content.length);

                Log.i("JSON", jsonParam.toString());
                try (OutputStream os = conn.getOutputStream()) {
                    os.write(content);
                }

我解决了我的问题。我不知道为什么,但是当我使用 Fiddler 时,我收到了这条消息。

谢谢大家