这到底是什么URLConnection.setDoOutput()的影响? [英] What exactly does URLConnection.setDoOutput() affect?

查看:564
本文介绍了这到底是什么URLConnection.setDoOutput()的影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还有 setDoOutput()的URLConnection 。据<一href="http://docs.oracle.com/javase/1.5.0/docs/api/java/net/URLConnection.html#setDoOutput%28boolean%29">documentation我应该

设置doOutput标志为true,如果你打算使用输出的URL连接,虚假的。

Set the DoOutput flag to true if you intend to use the URL connection for output, false if not.

现在我面临正是这个问题 - Java运行时将请求转换发表曾经 setDoOutput(真)被调用,服务器只响应 GET 请求。我想知道,如果我删除 setDoOutput(真)从code。

Now I'm facing exactly this problem - the Java runtime converts the request to POST once setDoOutput(true) is called and the server only responds to GET requests. I want to understand what happens if I remove that setDoOutput(true) from the code.

究竟这会不会影响?假设我将它设置为 - 我现在能做到什么我不能做呢?请问我能不能执行 GET 请求?什么是输出,在此方法的上下文

What exactly will this affect? Suppose I set it to false - what can I do now and what can't I do now? Will I be able to perform GET requests? What is "output" in context of this method?

推荐答案

您需要的,如果你想发送给它设置为true(输出的)请求主体,例如用POST或PUT请求。通过GET,你通常不会派一个机构,所以你不需要它。

You need to set it to true if you want to send (output) a request body, for example with POST or PUT requests. With GET, you do not usually send a body, so you do not need it.

发送请求体本身通过连接的输出流进行:

Sending the request body itself is done via the connection's output stream:

conn.getOutputStream().write(someBytes);

这篇关于这到底是什么URLConnection.setDoOutput()的影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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