Powered by Blogger.

Thursday 5 February 2015

URL Connection

No comments :
URL Connection The Java URLConnection class represents a communication link between the URL and the application. This class can be used to read and write data to the specified resource referred by the URL, create a URLConnection object from it, using its



It returns a URLConnection object associated with the invoking URL object. URLConnection defines several methods. Here is Sample:
int getContentLength()Returns the size in bytes of the content associated with the resource.
 if the length is unavailable, -1 is returned.
long getContentLengthLong()Returns the size in bytes of the content associated with the resource.
if the length is unavailable, -1 is returned.
String getContentType()Returns the type of content found in the resource. this is the value of the content-type header file. Returns null if the content type in not available.
long getDate()Returns the Time and date of  the response represented in terms of milliseconds since January 1,1970 GMT.
long getExpiration()Returns the Expiration time and date of the resource represented in terms of milliseconds since january 1,1970 GTM. Zero is returns if the expiration date unavailable.
String getHeaderField()Returns the value of the header field at index idx. (Header field indexes begin at 0) Returns null if the value of idx exceeds the number of fields.
String getHeaderField(String fieldNmae)Returns the header field whose name is specified by name. Returns null if the specified name is not found.
String getHeaderFieldKey(int idx)Returns the header field key at idx. (Header field indexes being at 0) Returns null if the value of idx exceeds the number of fields.
Map<String, Lits<String>> getHeaderField()Returns a Map that contains all the header fields and values.
long getLastModified()Returns the time and date, represented in terns of milliseconds since January 1, 1970 GTM, of  the last modification of the resource. Zero is returned if the last-modified date is unavailable.
InputStream getInputStream() throws IOException Returns the InputStream that is linked to the resource. this stream can be used to obtain the content if the resource.

The following example creates a URLConnection using the openConnection() method of a URL object and then uses it to examine the Document's properties and content.



Out Put:
Date : 
Thu Feb 05 22:02:55 IST 2015 
Expriation : Thu Feb 05 22:02:55 IST 2015 
Last-Modification : Thu Feb 05 22:02:48 IST 2015 
Content length unavilable 
===Content=== 


D










>...............

The Program establishes an HTTP connection to www.a143mk.blogspot.in. It then Displays Server header values and retrieves the content .you might find it interesting to try this example, observing the result, and then for comparison purposes try a different web site of your own choosing.

No comments :

Post a Comment

Please Write a Message for Programming or something Related issues.