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

[{'answer_id': 39431, 'author': 'WMR', 'author_id': 2844, 'author_profile': 'https://Stackoverflow.com/users/2844', 'pm_score': 2, 'selected': False, 'text': '<p>No it does not. But if the protocol of the URL is HTTP, you\'ll get a <a href="http://java.sun.com/javase/6/docs/api/java/net/HttpURLConnection.html" rel="nofollow noreferrer"><code>HttpURLConnection</code></a> as a return object. This class has a <code>setRequestMethod</code> method to specify which HTTP method you want to use. </p>\n\n<p>If you want to do more sophisticated stuff you\'re probably better off using a library like <a href="http://hc.apache.org/httpclient-3.x/" rel="nofollow noreferrer">Jakarta HttpClient</a>.</p>\n'}, {'answer_id': 39449, 'author': 'Herms', 'author_id': 1409, 'author_profile': 'https://Stackoverflow.com/users/1409', 'pm_score': 5, 'selected': True, 'text': '<p>If you retrieve the <code>URLConnection</code> object using <code>openConnection()</code> it doesn\'t actually start communicating with the server.  That doesn\'t happen until you get the stream from the <code>URLConnection()</code>.  When you first get the connection you can add/change headers and other connection properties before actually opening it.</p>\n\n<p>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 <code>setDoOutput(true)</code> 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.</p>\n\n<p>For example, this should do a POST:</p>\n\n<pre><code>URL myURL = new URL("http://example.com/my/path");\nURLConnection conn = myURL.openConnection();\nconn.setDoOutput(true);\nconn.setDoInput(true);\n\nOutputStream os = conn.getOutputStream();\nos.write("Hi there!");\nos.close();\n\nInputStream is = conn.getInputStream();\n// read stuff here\n</code></pre>\n\n<p>While this would do a GET:</p>\n\n<pre><code>URL myURL = new URL("http://example.com/my/path");\nURLConnection conn = myURL.openConnection();\nconn.setDoOutput(false);\nconn.setDoInput(true);\n\nInputStream is = conn.getInputStream();\n// read stuff here\n</code></pre>\n\n<p><code>URLConnection</code> will also do other weird things.  If the server specifies a content length then <code>URLConnection</code> will keep the underlying input stream open until it receives that much data, <em>even if you explicitly close it</em>.  This caused a lot of problems for us as it made shutting our client down cleanly a bit hard, as the <code>URLConnection</code> would keep the network connection open.  This probably probably exists even if you just use <code>getStream()</code> though.</p>\n'}]
['https://Stackoverflow.com/questions/39391', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4223/']