使用HttpURLConnection使用JSON数据发出PUT请求不起作用 [英] Making PUT request with JSON data using HttpURLConnection is not working

查看:111
本文介绍了使用HttpURLConnection使用JSON数据发出PUT请求不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java中的 HttpURLConnection 使用JSON数据发出PUT请求。我这样做的方式不起作用。我没有错误,所以我不知道问题是什么。

I'm trying to make PUT request with JSON data using HttpURLConnection in Java. The way I do it doesn't work. I get no errors so I don't know what the problem is.

public static void main(String[] args) {

        URL url;
        try {
            url = new URL("http://fltspc.itu.dk/widget/515318fe17450f312b00153d/");
            HttpURLConnection hurl = (HttpURLConnection) url.openConnection();
            hurl.setRequestMethod("PUT");
            hurl.setDoOutput(true);
            hurl.setRequestProperty("Content-Type", "application/json");
            hurl.setRequestProperty("Accept", "application/json");

            String payload = "{'pos':{'left':45,'top':45}}";

            OutputStreamWriter osw = new OutputStreamWriter(hurl.getOutputStream());
            osw.write(payload);
            osw.flush();
            osw.close();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

以下是我实际尝试的请求make:

And here is the request I'm actually trying to make:

我已经在同一个应用程序中对资源发出GET请求,但它运行正常。我将非常感谢有关如何调试的所有提示或如何尝试以其他方式执行此操作。到目前为止,我只尝试使用 OutputStream 而不是 OutputStreamWriter ,但它既不起作用。

I was already making GET requests to the resource within the same app and it worked fine. I would be very grateful for all tips on how can I debug that or how can I try to do it some other way. So far I tried only using OutputStream instead of OutputStreamWriter but it doesn't work neither.

推荐答案

HttpURLConnection的Sun(Oracle)实现会缓存帖子的内容,除非您告诉它处于流模式。如果您开始与响应进行交互,则会发送内容,例如:

The Sun (Oracle) implementation of HttpURLConnection caches the content of your post unless you tell it to be in streaming mode. The content will be sent if you start interaction with the response such as:

hurl.getResponseCode();

此外,根据RFC 4627,您不能在json中使用单引号(尽管某些实现似乎不小心)。

Also, according to RFC 4627 you can not use single quotes in your json (although some implementations seem to not care).

因此,将您的有效负载更改为:

So, change your payload to:

String payload = "{\"pos\":{\"left\":45,\"top\":45}}";

此示例适用于我

public class HttpPut {
    public static void main(String[] args) throws Exception {
        Random random = new Random();
        URL url = new URL("http://fltspc.itu.dk/widget/515318fe17450f312b00153d/");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("PUT");
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept", "application/json");
        OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
        osw.write(String.format("{\"pos\":{\"left\":%1$d,\"top\":%2$d}}", random.nextInt(30), random.nextInt(20)));
        osw.flush();
        osw.close();
        System.err.println(connection.getResponseCode());
    }
}

这篇关于使用HttpURLConnection使用JSON数据发出PUT请求不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆