Java new URL(...).openConnection() 是否一定意味着 POST?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:29:57  来源:igfitidea点击:

Does new URL(...).openConnection() necessarily imply a POST?

javahttpurl

提问by jodonnell

If I create an HTTP java.net.URLand then call openConnection()on it, does it necessarily imply that an HTTP post is going to happen? I know that openStream()implies a GET. If so, how do you perform one of the other HTTP verbs without having to work with the raw socket layer?

如果我创建一个 HTTPjava.net.URL然后调用openConnection()它,它是否一定意味着一个 HTTP post 会发生?我知道这openStream()意味着 GET。如果是这样,您如何在不使用原始套接字层的情况下执行其他 HTTP 动词之一?

采纳答案by Herms

If you retrieve the URLConnectionobject using openConnection()it doesn't actually start communicating with the server. That doesn't happen until you get the stream from the URLConnection(). When you first get the connection you can add/change headers and other connection properties before actually opening it.

如果您URLConnection使用openConnection()它检索对象实际上并没有开始与服务器通信。直到您从URLConnection(). 当您第一次获得连接时,您可以在实际打开它之前添加/更改标题和其他连接属性。

URLConnection's life cycle is a bit odd. It doesn't send the headers to the server until you've gotten one of the streams. If you just get the input stream then I believe it does a GET, sends the headers, then lets you read the output. If you get the output stream then I believe it sends it as a POST, as it assumes you'll be writing data to it (You may need to call setDoOutput(true)for the output stream to work). As soon as you get the input stream the output stream is closed and it waits for the response from the server.

URLConnection 的生命周期有点奇怪。在您获得其中一个流之前,它不会将标头发送到服务器。如果您只是获取输入流,那么我相信它会执行 GET,发送标头,然后让您读取输出。如果您获得输出流,那么我相信它会将其作为 POST 发送,因为它假定您将向其写入数据(您可能需要调用setDoOutput(true)输出流才能工作)。一旦您获得输入流,输出流就会关闭并等待来自服务器的响应。

For example, this should do a POST:

例如,这应该做一个 POST:

URL myURL = new URL("http://example.com/my/path");
URLConnection conn = myURL.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);

OutputStream os = conn.getOutputStream();
os.write("Hi there!");
os.close();

InputStream is = conn.getInputStream();
// read stuff here

While this would do a GET:

虽然这会做一个 GET:

URL myURL = new URL("http://example.com/my/path");
URLConnection conn = myURL.openConnection();
conn.setDoOutput(false);
conn.setDoInput(true);

InputStream is = conn.getInputStream();
// read stuff here

URLConnectionwill also do other weird things. If the server specifies a content length then URLConnectionwill keep the underlying input stream open until it receives that much data, even if you explicitly close it. This caused a lot of problems for us as it made shutting our client down cleanly a bit hard, as the URLConnectionwould keep the network connection open. This probably probably exists even if you just use getStream()though.

URLConnection还会做其他奇怪的事情。如果服务器指定了内容长度,URLConnection则将保持底层输入流打开,直到它接收到那么多数据,即使您明确关闭它。这给我们带来了很多问题,因为它让我们的客户端彻底关闭有点困难,因为这URLConnection会保持网络连接打开。即使您只是使用,这也可能存在getStream()

回答by WMR

No it does not. But if the protocol of the URL is HTTP, you'll get a HttpURLConnectionas a return object. This class has a setRequestMethodmethod to specify which HTTP method you want to use.

不,不是的。但是如果 URL 的协议是 HTTP,你会得到 aHttpURLConnection作为返回对象。这个类有一个setRequestMethod方法来指定你想要使用的 HTTP 方法。

If you want to do more sophisticated stuff you're probably better off using a library like Jakarta HttpClient.

如果你想做更复杂的事情,你可能最好使用像Jakarta HttpClient这样的库。