All About Class URLConnection in Java



The abstract class URLConnection is the super class of all the classes that represent a communication link between the application and a URL.Instances of this class can be used both to read from and to write to the resource reference by the URL.In general,creating a connection to a URL is a multi step process.
They are as follows:

  1. The connection object is created by invoking openConnection() on a URL.
  2. The setup parameters are manipulated and the actual connection to remote object is available for use.
  3. The remote object is available for use finally.
There are a set of properties have a setter and a getter method.The first three in the before column also have corresponding default properties that specify the default values used in the subsequent requests where the properties are not specified.In common case,all of these properties can be ignored as the pre connection properties default to sensible values and the post condition properties are often uninteresting.For more clients of this interface, there are only two interesting methods.They are:
  1.  getInputStream()
  2. getObject()
They are mirrored in the URL class by convenience method.

The structure of the class URLConnection is given as:

public abstract class java.net.URLConnection extends java.lang.Object{
//member element
protected boolean allowUserInteraction;
protected boolean connected;
protected boolean doInput;
protected boolean doOutput;
protected long iModifiedSince;
protected URL url;
protected boolean useCaches;
//constructor
protected URLConnection(URL url);
//construct a URL connection to the specified URL.A connection to the object referenced by the URL is created.
//methods
public abstract void connect();
public boolean getAllowUserInteraction();
public boolean getDefaultUseCaches();
public boolean getDoInput();
public boolean getDoOutput();
public boolean getUseCaches();
public static boolean getDefaultAllowUserInteraction();
public Object getContect();
public String getContentEncoding();
public String getContentType();
public String getHeaderField(int n);
public String getHeaderField(String name);
public String getHeaderFieldKey(int n);
public String getRequestProperty(string key);
public String toString();
public static String getDefaultRequestProperty(String key);
public static String guessContentTypeFromName(String fname);
public static String guessContentTypeFromStream(InputStream in);
public int getContentLength();
public long getHeaderFieldInt(String name,long default);
public long getDate();
public long getExpiration();
public long getHeaderFieldDate(String name,long default);
public long getIfModifiedSince();
public long getLastModified();
public InputStream getOutputStream();
public URL getURL();
public void setAllowUserInteraction(boolean allow);
public static void setContentHandlerFactory(ContentHandlerFactory fac);
public static void setDefaultAllowUserInteraction(boolean default);
public static void setDefaultRequestProperty(String key,String value);
public void setDefaultUseCaches(boolean uCaches);
public void setDoinput(boolean doinput);
public void setDoOutput(boolean dooutput);
public void setIfModifiedSince(long modify);
public void setRequestProperty(String key,String value);
public void setUseCaches(boolean uCaches);
}
 
protected boolean allowUserInteraction Details
The value of this field can be set by the setAllowUserInteraction() method.Its value can be accessed by the getAllowUserInteraction() method.Its default value is the value of the argument method to the last call to the setDefaultAllowUserInteraction() method.
if true this url will be examined to allow user interactions such as popping up an authentication dialog. If false,then no user interaction is allowed.
protected boolean connected Details
If false,this connection object has not created a communication link to the specified URL.If true,the communication link has been established.
protected boolean doInput Details
This variable is set by the setDoInput() method.Its value can be accessed by the getDoInput() method.if doInput flag is set to true,indicates the application intends to read data from the URL connection.The default value of this field is true.
protected boolean doOutput Details
This variable is set by the setDoOutput() method.Its value can be accessed by getDoInput() method.A URL connection can be used for input and/or output.Setting the doOutput flag to true indicates that the application intends to write data to URLConnection.The default value is set to false for this field.
protected long iModifiedSince Details
some protocols skip the fetching of the object unless the object has been modified more recently than a certain time.A non zero value is interpreted as the time corresponding to if modification since seconds since January 1,1970,GMT.The object is fetched only if it has been modified more recently than that time.This variable is set by the set if modifiedSince() method.Its value can be accessed by the getIfModifiedSince() method.The fetching must always occur.
protected URL url Details
The url representing the remote object on the world wide web to which this connection is opened.The value of this field can be accessed by the getURL() method.The default value of this variable is teh value of the URL argument in the URLConnection constructor.
protected boolean useCaches Details
This method is to set by the setUseCaches() method.It value can be accessed by the getUseCache() method.Its default value is the value of the last argument to the method setDefaultUseCaches().If true,the protocol is allowed to use caching whenever it can.If false,the protocol must always try to get a fresh copy of the object.
All About Class URLConnection in Java All About Class URLConnection in Java Reviewed by Animesh Chatterjee on October 18, 2018 Rating: 5

No comments:

Powered by Blogger.