Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Whether the server closes the connection or not, depends on whether the client request asks for the connection to close or not.</p> <p>Here is a sample Server code:</p> <pre><code>import org.restlet.data.Form; import org.restlet.data.MediaType; import org.restlet.data.Parameter; import org.restlet.resource.Get; import org.restlet.resource.ServerResource; import org.restlet.util.Series; public class TestRestlet extends ServerResource { @Get public String getImpl(){ return "Sample Response Text\r\n"; } } </code></pre> <p>And here is what i got on linux commmand line (using only telnet): [Please note that the last line in request-header in each request is followed by 2 <i>newlines</i>] [To avoid any confusion, some of the requests do not contain request-body.]</p> <pre><code>[root@mylinuxserver]# telnet 172.16.101.34 6060 Trying 172.16.101.34... Connected to win7comp01 (172.16.101.34). Escape character is '^]'. GET /TestRestlet HTTP/1.1 Host: 172.16.101.34:6060 HTTP/1.1 200 OK Set-Cookie: JSESSIONID=C2E77F4D0437E525A0FC66498EF09E8B; Path=/hotelSoft Date: Wed, 31 Jul 2013 08:25:44 GMT Accept-Ranges: bytes Server: Restlet-Framework/2.0.15 Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept Content-Type: application/json;charset=UTF-8 Content-Length: 22 Sample Response Text GET /TestRestlet HTTP/1.1 Host: 172.16.101.34:6060 Connection: Keep-Alive HTTP/1.1 200 OK Set-Cookie: JSESSIONID=1873DE26443F5DF62379B895AEA0F004; Path=/hotelSoft Date: Wed, 31 Jul 2013 08:25:48 GMT Accept-Ranges: bytes Server: Restlet-Framework/2.0.15 Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept Content-Type: application/json;charset=UTF-8 Content-Length: 22 Sample Response Text GET /TestRestlet HTTP/1.1 Host: 172.16.101.34:6060 Connection: close HTTP/1.1 200 OK Set-Cookie: JSESSIONID=43EC7C9AACC6C0CEF6FAC8F608B1D79C; Path=/hotelSoft Date: Wed, 31 Jul 2013 08:25:57 GMT Accept-Ranges: bytes Server: Restlet-Framework/2.0.15 Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept Content-Type: application/json;charset=UTF-8 Content-Length: 22 Connection: close Sample Response Text Connection closed by foreign host. [root@mylinuxserver]# telnet 172.16.101.34 6060 Trying 172.16.101.34... Connected to win7comp01 (172.16.101.34). Escape character is '^]'. GET /TestRestlet HTTP/1.0 HTTP/1.1 200 OK Set-Cookie: JSESSIONID=2C879A91F2501DD9D3B39EF50C3F46CA; Path=/hotelSoft Date: Wed, 31 Jul 2013 08:26:09 GMT Accept-Ranges: bytes Server: Restlet-Framework/2.0.15 Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept Content-Type: application/json;charset=UTF-8 Content-Length: 22 Connection: close Sample Response Text Connection closed by foreign host. [root@mylinuxserver]# telnet 172.16.101.34 6060 Trying 172.16.101.34... Connected to win7comp01 (172.16.101.34). Escape character is '^]'. GET /TestRestlet Sample Response Text Connection closed by foreign host. [root@mylinuxserver]# </code></pre> <p>In the above examples, several types of HTTP connections are made.</p> <p>The response to the 1st request:</p> <pre><code>GET /TestRestlet HTTP/1.1 Host: 172.16.101.34:6060 </code></pre> <p>[Note: the line <code>Host: 172.16.101.34:6060</code> is followed by 2 <code>\r\n</code>: <code>\r\n\r\n</code>]</p> <p>is:</p> <pre><code>HTTP/1.1 200 OK Set-Cookie: JSESSIONID=C2E77F4D0437E525A0FC66498EF09E8B; Path=/hotelSoft Date: Wed, 31 Jul 2013 08:25:44 GMT Accept-Ranges: bytes Server: Restlet-Framework/2.0.15 Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept Content-Type: application/json;charset=UTF-8 Content-Length: 22 Sample Response Text </code></pre> <p>The connection is not closed yet, and we send another request:</p> <pre><code>GET /TestRestlet HTTP/1.1 Host: 172.16.101.34:6060 Connection: Keep-Alive </code></pre> <p>That gets the response:</p> <pre><code>HTTP/1.1 200 OK Set-Cookie: JSESSIONID=1873DE26443F5DF62379B895AEA0F004; Path=/hotelSoft Date: Wed, 31 Jul 2013 08:25:48 GMT Accept-Ranges: bytes Server: Restlet-Framework/2.0.15 Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept Content-Type: application/json;charset=UTF-8 Content-Length: 22 </code></pre> <p>Still the connection is not closed.</p> <p>However on the 3rd request:</p> <pre><code>GET /TestRestlet HTTP/1.1 Host: 172.16.101.34:6060 Connection: close </code></pre> <p>The connection is closed, because the request contains <code>Connection: close</code> header. You can see the telnet exits after displaying a message: <code>Connection closed by foreign host.</code></p> <p>There are 2 more sample request-response in the above given examples:</p> <p>1.An HTTP 1.0 request:</p> <pre><code>GET /TestRestlet HTTP/1.0 </code></pre> <p>With response:</p> <pre><code>HTTP/1.1 200 OK Set-Cookie: JSESSIONID=2C879A91F2501DD9D3B39EF50C3F46CA; Path=/hotelSoft Date: Wed, 31 Jul 2013 08:26:09 GMT Accept-Ranges: bytes Server: Restlet-Framework/2.0.15 Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept Content-Type: application/json;charset=UTF-8 Content-Length: 22 Connection: close Sample Response Text </code></pre> <p>And the telnet exits after displaying: <code>Connection closed by foreign host.</code></p> <p>2.A request without HTTP version mentioned:</p> <pre><code>GET /TestRestlet </code></pre> <p>And response is: (Without headers)</p> <pre><code>Sample Response Text </code></pre> <p>And the telnet exits with a message: <code>Connection closed by foreign host.</code></p> <p>Conclusion:</p> <p>Whatever is your client / client-program , make it send an HTTP-1.0 request , or a HTTP-1.1 request with <code>Connection: close</code> header.</p> <p>In Java, you achieve this by:</p> <pre><code>import java.net.HttpURLConnection; import java.net.URL; . . . HttpURLConnection httpURLConnection = (HttpURLConnection) new URL("http://....").openConnection(); httpURLConnection.setRequestProperty("Connection","close"); // rest of the code here.... </code></pre> <p>Also check if a statement like this:</p> <pre><code>httpURLConnection.disconnect(); </code></pre> <p>can help you disconnect the connection.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload