All About Class URLConnection in Java

0
187
Spread the love
  • 29
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    138
    Shares

Class URLConnection in Java
Class URLConnection in Java

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 openConnection() method returns a java.net.URLConnection,an abstract class whose subclasses represent the various types of URL connections.

Example:

  • If we try to connect an URL whose protocol is HTTP, the openConnection() method returns an HttpURLConnection object.
  • If we try to connect an URL whose representation is jar, the openConnection() method returns an JarURLConnection object.

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 getContent();//returns the content of the URL connection
public Object getContent(Class[] classes);//returns the contents of the URL connection
public String getContentEncoding();//returns the value of the content encoding header field.
public String getContentType();//returns the value of the content type header field.
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();//returns the value of the content type header field.
public long getHeaderFieldInt(String name,long default);
public long getDate();
public long getExpiration();//returns the value of the expired header field.
public long getHeaderFieldDate(String name,long default);
public long getIfModifiedSince();//returns the value of this object's ifModifiedSince field.
public long getLastModified();//Returns the value of the last modified header field.
public InputStream getInputStream()throws IOException;//returns the input stream of the URL 
//connection for reading from resource.
public OutputStream getOutputStream() throws IOException//returns the output stream of the URL 
//connection for writing to the resource.
public URL getURL();//returns the URL that this URLConnection object is connected to.
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);//returns true if the connection is used for input
//The default value id true because clients typically read from an URL connection.
public void setDoOutput(boolean dooutput);//returns true if the connection is used for output.
//The default value id false because many types of URLs do not support being written to.
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.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 29
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    138
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here