Configuring the Connection

The URLConnection class has seven protected instance fields that define exactly how the client will make the request to the server. These are:

protected URL     url;
protected boolean doInput = true;
protected boolean doOutput = false;
protected boolean allowUserInteraction = defaultAllowUserInteraction;
protected boolean useCaches = defaultUseCaches;
protected long    ifModifiedSince = 0;
protected boolean connected = false;

For instance, if doOutput is true, then you’ll be able to write data to the server over this URLConnection as well as read data from it. If useCaches is false, the connection will bypass any local caching and download the file from the server afresh.

Since these fields are all protected, their values are accessed and modified via obviously named setter and getter methods:

public URL     
            getURL(  )
public void    setDoInput(boolean doInput)
public boolean getDoInput(  )
public void    
            
            setDoOutput(boolean doOutput)
public boolean getDoOutput(  )
public void    setAllowUserInteraction(boolean allowUserInteraction)
public boolean getAllowUserInteraction(  )
public void    
            
            
            setUseCaches(boolean useCaches)
public boolean getUseCaches(  )
public void    setIfModifiedSince(long ifModifiedSince)
public long    getIfModifiedSince(  )

You can modify these fields only before the URLConnection is connected (that is, before you try to read content or headers from the connection). Most of the methods that set fields throw an IllegalAccessError if they are called while the connection ...

Get Java Network Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.