Java FormData请求为空

时间:2019-04-27 09:36:20

标签: java rest request

我正在呼叫一个第三方请求,因此我必须使用java发送表单数据:

URL url = new URL("http://192.145.0.17:5000/callDay");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            conn.setUseCaches(false);
            conn.setDoOutput(true); // indicates POST method
            conn.setDoInput(true);

            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Cache-Control", "no-cache");

            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestProperty(
                    "Content-Type", "multipart/form-data; boundary=--******");

            OutputStream os = conn.getOutputStream();
            BufferedWriter httpRequestBodyWriter =
                    new BufferedWriter(new OutputStreamWriter(os));


            httpRequestBodyWriter.write("\n\n--******\n");
            httpRequestBodyWriter.write("Content-Disposition: form-data;"
                    + "name=\"user_id\""
                    + "\n Admin");

            httpRequestBodyWriter.write("\n\n--******\n");
            httpRequestBodyWriter.write("Content-Disposition: form-data;"
                    + "name=\"abc\";"
                    + "filename=\"abc\""
                    + "\nContent-Type: \n\n");
            httpRequestBodyWriter.write("\n\n--******\n");
            httpRequestBodyWriter.write("Content-Disposition: form-data;"
                    + "name=\"ouput_path\""
                    + "\n dc");

            httpRequestBodyWriter.flush();

            os.write(Files.readAllBytes(Paths.get(nbPath)));


            os.flush();

            // Mark the end of the multipart http request
            httpRequestBodyWriter.write("\n--******--\n");
            httpRequestBodyWriter.flush();

            os.close();
            httpRequestBodyWriter.close();

            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + conn.getResponseCode());
            }

,此请求来自python端点,但是当python尝试从数据读取请求时,它总是获取空白数据。为什么它获取空白数据?

对不起,我的英语。请帮帮我。

0 个答案:

没有答案