简体   繁体   English

带有HttpUrlConnection的PUT请求(JAVA)

[英]PUT request (JAVA) with HttpUrlConnection

I'm trying to do a "PUT" request with Android Studio. 我正在尝试使用Android Studio发出“ PUT”请求。 But, actually, it doesn't work, I received a "404 not found" whereas I got all the needed infos. 但是,实际上,这是行不通的,我收到了“找不到404”的信息,而我却得到了所有需要的信息。


String url = " https://[...] "; 字符串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();
        }

If I try the request on Symfony (web app), it's worked well, but with the code, not anymore... Do you see any problems with my request ? 如果我在Symfony(Web应用程序)上尝试该请求,则效果很好,但是使用了代码,现在就不行了...您是否发现我的请求有任何问题?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM