This content has been marked as final. Show 10 replies
when u call the getInputStream() method, the URLConnection instance opens a connection and issues a request if not issued allready earlier (by a call to the connect() method for example).
As george wrote, at the server side, your web container simply invokes the appropriate servlet.
Nothing too interesting about it ;-)
what is openStream?
Whatever that is, u should call getInputStream() only when you're done sending data to the server, do all u need to do with the connection and then call getInputStream().
URLConnection, or HttpURLConnection in this case is of course based on sockets and adds all the prtocol related functionnality, it has nothing to do with "techniques". It encapsulates the HTTP protocol over a socket connection, and exposes all u need, the usage obviously is simply a little different from a plain socket....
Thank you, I understand it!
I just forgot to invoke connect() method:
look to it specification
public abstract void connect() throws IOException
After connecting, it is an error to try to set them. Operations that depend on being connected, like getContentLength, will implicitly perform the connection, if necessary.
My last investigation:
Here is updated code source:
As I noticed when you write bytes in output stream they are stored in buffer and doesn't send to server right now.
URL url = new URL("http://localhost:8080/proxy/servlet/servlet.ProxyServlet"); URLConnection connection = url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); connection.connect(); OutputStream out = connection.getOutputStream(); out.write("hello world".getBytes()); out.close(); ((HttpURLConnection) connection).getResponseCode(); //attention InputStream in = connection.getInputStream(); int first = in.read(); in.close();
(See HttpURLConnection API description)
But when, in my first topic example, I tried to receive input stream, method service() had invoked on servlet.
Do you know why? Because, in this time buffer flushed and data sent to server. I noticed, that direct flush() method invoking on output stream are not produce this effect. So, I noticed that buffer also flushed when you invoke method getResponseCode() (see code string with comment attention)
and then you may work with input stream. Now I'm interesting, do you now aesthetically beautiful method which may send output stream buffer to server, except getResponseCode() invoking?
Any method u call that requires the server's response will flush all data left on the client side buffer. The flush operation may be called several times (if u write a lot of data), or only once when the connection object decides u r completely done with sending data (like in the case of trying to obtain any part of the response - getResponseCode, getInputStream, etc.).
The nice thing about it here is that u shouldn't care. It's implementation specific and part of the idea behind abstraction - u r using an object representing an http connection, u shouldn't deal nor care about technical stuff like when the data is flushed.
I doubt it'll count automatically, if it does it's implementation specific and not required, so don't rely on it.
Moreover it's only for requests that have a body (which means u don't need it for a GET request).
I believe u set it by calling setRequestProperty() method.
All this aside, If u ask me, I'd advise u to use apache's http client open source project, however imperfect it is (at least the last version I used about a year ago) it's better and more intuitive than the JDK classes.