Java Reference
In-Depth Information
public void setDefaultUseCaches ( boolean useCaches )
public boolean getDefaultUseCaches ()
Although nonstatic, these methods do set and get a static field that determines the default
behavior for all instances of the URLConnection class created after the change. The next
code fragment disables caching by default; after this code runs, URLConnection s that
want caching must enable it explicitly using setUseCaches(true) :
if ( uc . getDefaultUseCaches ()) {
uc . setDefaultUseCaches ( false );
}
Timeouts
Four methods query and modify the timeout values for connections; that is, how long
the underlying socket will wait for a response from the remote end before throwing a
SocketTimeoutException . These are:
public void setConnectTimeout ( int timeout )
public int getConnectTimeout ()
public void setReadTimeout ( int timeout )
public int getReadTimeout ()
The setConnectTimeout() / getConnectTimeout() methods control how long the sock‐
et waits for the initial connection. The setReadTimeout() / getReadTimeout() methods
control how long the input stream waits for data to arrive. All four methods measure
timeouts in milliseconds. All four interpret zero as meaning never time out. Both setter
methods throw an IllegalArgumentException if the timeout is negative.
For example, this code fragment requests a 30-second connect timeout and a 45-second
read timeout:
URL u = new URL ( "http://www.example.org" );
URLConnuction uc = u . openConnection ();
uc . setConnectTimeout ( 30000 );
uc . setReadTimeout ( 45000 );
Configuring the Client Request HTTP Header
An HTTP client (e.g., a browser) sends the server a request line and a header. For
example, here's an HTTP header that Chrome sends:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Cookie:reddit_first=%7B%22firsttime%22%3A%20%22first%22%7D
DNT:1
Search WWH ::




Custom Search