Java HttpURLConnection OutputStreamWriter为空 [英] Java HttpURLConnection OutputStreamWriter is empty

查看:151
本文介绍了Java HttpURLConnection OutputStreamWriter为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被回答了10,000多次.我已经阅读并测试了一个多小时的建议,没有任何进展.

I know this has been answered 10,000+ times. I have been reading and testing recommendations for over an hour without any progress.

在下面的代码中,请求正文始终为空.

In the code below, the request body is always empty.

URL url = new URL("[REMOVED]");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestProperty("Authorization", "Bearer [REMOVED]");
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/json");
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write("ABC");
writer.flush();
writer.close();

String line;
BufferedReader reader = new BufferedReader(new 
                             InputStreamReader(conn.getInputStream()));
while ((line = reader.readLine()) != null) {
  System.out.println(line);
}

reader.close();

记录是在服务器上创建的,但正文始终为空.

The record is created on the server, but the body is always null.

感谢您的帮助.

推荐答案

与在localhost上不起作用的完全相同的代码在App Engine上起作用.我不明白为什么,但是上面的代码有效.

The exact same code that does not work on localhost works on app engine. I don't understand why, but the code above works.

这篇关于Java HttpURLConnection OutputStreamWriter为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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