新的URL(...)。openConnection()是否必然意味着POST? [英] Does new URL(...).openConnection() necessarily imply a POST?

查看:106
本文介绍了新的URL(...)。openConnection()是否必然意味着POST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我创建了一个HTTP java.net.URL ,然后在其上调用 openConnection(),那么它必然意味着HTTP帖子会发生?我知道 openStream()意味着GET。如果是这样,如何在不使用原始套接字层的情况下执行其他HTTP谓词之一?

If I create an HTTP java.net.URL and 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?

推荐答案

如果你使用 openConnection()检索 URLConnection 对象它实际上并不开始与服务器通信。直到从 URLConnection()获取流后才会发生这种情况。当您第一次获得连接时,您可以在实际打开它之前添加/更改标题和其他连接属性。

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

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.

例如,这应该执行POST:

For example, this should do a 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

虽然这样做可以获得GET:

While this would do a 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

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

URLConnection will also do other weird things. If the server specifies a content length then URLConnection will 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 URLConnection would keep the network connection open. This probably probably exists even if you just use getStream() though.

这篇关于新的URL(...)。openConnection()是否必然意味着POST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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