Java HttpURLConnection OutputStreamWriter is empty

馋奶兔 提交于 2019-12-12 06:37:12

问题


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.

Any help is appreciated.


回答1:


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



来源:https://stackoverflow.com/questions/30818627/java-httpurlconnection-outputstreamwriter-is-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!