How to check if URLConnection is already in connected state?
<p>I want to check the connected state of a URLConnection in Java.</p>

<p>I want to write a <code>toString(URLConnection)</code> method to print all the header-data (Request &amp; Response) of the connection.</p>

<p>But the <code>toString()</code> method should only print the Response-data when the connection is already established!</p>

<p>The problem here is that the connection is established once any getter or method is called that accesses the response data.</p>

<p>Thus i can not simply call getHeaderFields() since this would actually trigger the connect. This helper method should not influence the internal state of the URLConnection. It should only passively read the data from it (that is already there and skip the parts that are not loaded yet.</p>

<p>So in short i need a way to check if the URLConnection is in connected state already before accessing any method that triggers connect().</p>