PUT 请求 (JAVA) 与 HttpUrlConnection

PUT request (JAVA) with HttpUrlConnection

我正在尝试使用 Android Studio 执行 "PUT" 请求。 但是,实际上,它不起作用,我收到了“404 not found”,而我得到了所有需要的信息。


字符串url = "https://[...]";

        URL obj = null;
        try {
            obj = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }


        HttpURLConnection con = null;
        DataOutputStream dataOutputStream = null;
        try {
            con = (HttpURLConnection) obj.openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // optional default is GET
        try {
            con.setRequestMethod("PUT");
            con.setDoInput(true);
            con.setDoOutput(true);
            System.out.println("Info User to update : " + params[0]);

            con.setRequestProperty("X-Auth-Token", params[0].get("token"));
            [...]

            con.setRequestProperty("shop_id", params[1].get("id"));

            dataOutputStream = new DataOutputStream(con.getOutputStream());
            OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream());
            osw.flush();
            osw.close();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (dataOutputStream != null) {
                try {
                    dataOutputStream.flush();
                    dataOutputStream.close();
                } catch (IOException exception) {
                    exception.printStackTrace();
                }
            }
        }

        int responseCode = 0;
        try {
            responseCode = con.getResponseCode();
        } catch (IOException e) {
            e.printStackTrace();
        }

如果我在 Symfony(Web 应用程序)上尝试请求,它运行良好,但使用代码,就不行了......你发现我的请求有任何问题吗?

问题已解决,我可以使用 PATCH 请求来做我想做的事情。