Powered by Blogger.

Wednesday 11 February 2015

HTTP URL

No comments :
HttpURLConnection :
Java provides a subclass of URLConnection that provides support for HTTP connections. This class is called HttpURLConnection. You obtain an HttpURLConnection in the same way just shown, by calling openConnection() on a URL object, but you must cast the result to HttpURLConnection. you can also use any of the several Methods defined by HttpURLConnection.

static boolean getFollowRedirects()Returns true if redirects are automatically followed and false otherwise. This feature is on by default.
String getRequestMethod()Returns a string representing how URL requests are made. The default is GET. Other options, such as Post, are available.
int getResponseCode() throws IOExceptionReturns the HTTP response code -1 is returned if no response code can be obtained. As IOException is thrown if the connection fails.
String getResponseMessage() throws IOException Returns the response message associated with the response code. Returns NULL if no message is available. An IOException is throws if the connection fails.
static void setFollowRedirects(boolean how)if how is true, then redirects are automatically followed. if how is false, redirects are not automatically followed. By default, redirects are automatically followed.
void setRequestMethod(String how) throws ProtocolExceptionSet the method by which HTTP requests are made to that specified by how. the default method is GET, but other options, such as POST, are available. if how is invalid, a ProtocolException is thrown.

The Following program demonstrates HttpURLConnection. It first establishes a connection to "www.a143mkblogspot.in". Then it displays the request method, the response code and The Response Message.



Out Put:
Request Method is :GET 
Response Code is : 200 
Response Message is : OK 
Here is the Header : Key : 
Transfer-EncodingValues[chunked] 
Key : nullValues[HTTP/1.1 200 OK] 
Key : ServerValues[GSE] 
Key : X-Content-Type-OptionsValues[nosniff] 
Key : Last-ModifiedValues[Wed, 11 Feb 2015 10:56:58 GMT] 
Key : DateValues[Wed, 11 Feb 2015 10:57:00 GMT] 
Key : Alternate-ProtocolValues[80:quic,p=0.02] 
Key : Accept-RangesValues[none] 
Key : Cache-ControlValues[private, max-age=0] 
Key : VaryValues[Accept-Encoding] 
Key : ExpiresValues[Wed, 11 Feb 2015 10:57:00 GMT] 
Key : X-XSS-ProtectionValues[1; mode=block] 
Key : Content-TypeValues[text/html; charset=UTF-8] 

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.

Monday 2 February 2015

URL

No comments :
URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet. The URL class provides a simple, concise API to access information across the Internet using URLs. A URL has four main components:The first is the protocol to use, separated from the reset of the locator by a colon (;). Component protocols are HTTP, FTP, gopher, and file, although these days almost everything is being done via HTTP (in fact, most browsers will proceed correctly if you leave off the "http://" form your URL specification). The second component is the host name or IP address of the host to use; this is delimited on the left by double slashes (//) and on the right by a slashes (/ ) or optional a colon (;). The third component, the port number, is an optional parameter, delimited on the left form the host name by a cokon (:) and on the right by a slash (/). (It defaults to port 80, the predefined HTTP port; thus, ":80" is redundant.) The fourth part is the actual file path. Most HTTP servers will append a file named index.html or index.htm is the to urls that refer directly to a directory resource. Thus, http://bindprogram.com/ is the same as http://bindprogram.com/index.htm. Java's URL class has several constructors; each can throw a MalformedURLEcxeption. One commonly used form specifies the URL, with a string that is identical to what you see displayed in a browser:


The Next two forms of the constructor allow you to break up the URL into its component parts;



another frequently used constructor allows you to use an existing URL, as a reference context and then create a new URL. from the context. Although this sounds a little contorted,it's really quite easy and useful.



The Following Exampal creates a URL to page on bindprogram.com.



Out Put:
                                    String : http://a143mk.blogspot.in/Help 
                                    Host : a143mk.blogspot.in 
                                    Port : -1 
                                    File : /Help 
                                    Protocol : http
Notice: That the port is -1; this means that a port was not explicitly set.Given URL object, you can retrieve the data associated with it. to access the actual bits or content information of a URL.

Saturday 31 January 2015

Java Sockets Programming

No comments :
There are two kinds of TCP sockets in java. One is for servers, and the other is for clients. The ServerSocket class is Designed to be a "Listener", which whats for clients to connect before doing anything. Thus, ServerSocket is for servers. The socket class is for clients. it is designed to connect to server sockets and initiate protocol exchanges. Because client sockets are the most commonly used by java application, they Examined here. Here are tow constructors used to create client sockets.

Socket(String hostName) throws UnkownHostException, IOException Creates a socket Connected to the Named Host and Port. 
Socket(InetAddress ipAddress, int port) throws IOException Creates a socket using a Preexisting InetAddress Object and port. 

Socket Defines Several instance Methods

InetAddress getInetAddress() Returns the InetAddress associated with the socket object. it returns null if the socket is not connected.
int getPort() Returns the remote Port to which invoking socket object is connected. it returns 0 if the socket is not connected.  
int getlocalport() Returns the local port to which the invoking socket object is bound. it returns -1 if the socket is not bound.

You can gain access to the input and output streams associated with a Socket by use of the getInputStream() and getOutputStream() method, as shown here. Each can throw an IOException if the socket has been invalidated by a loss of connection. These streams are used exactly like the I/O Streams to send and receive data .

InputStream getInputStream() throws IOExceptionReturns the InputStream associated with the invoking socket. 
OutputStream getOutputStream() throws IOException Returns the OutputStream associated with the invoking socket.

Several other methods are available,Including Connect(), Which allows you to specify a new Connection; isConnected(), which returns true if the socket is connected to a server; isBound(), which returns true if the socket is bound to an address; and isClosed(), which is returns true if the socket is Closed. To Close a socket, call close(). Closing a socket also closes the I/O Stream associated with the socket, call close(). Closing a Socket also closes the I/O Stream associated with the socket. The Following Program Provides a Simple Socket Example.



Out Put:
Whois Server Version 2.0 
Domain names in the .com and .net domains can now be registered 
with many different competing registrars. Go to http://www.internic.net for detailed information. Domain Name: MHPROFESSIONAL.COM
Registrar: CSC CORPORATE DOMAINS, INC. 
Sponsoring Registrar IANA ID: 299 
Whois Server: whois.corporatedomains.com 
Referral URL: http://www.cscglobal.com/digitalbrandservices 
Name Server: PDNS85.ULTRADNS.BIZ 
Name Server: PDNS85.ULTRADNS.COM.

Friday 30 January 2015

Java Networking

No comments :
Basics Networking: Computers Running on the Internet Communication to each other using either the TCP OR UDP.Java Network Programming:Network Programming refers to writing Programs that execute across multiple devices in which the devices are all connected to each using a network. The Networking Classes and Interfaces available in java.net package.
Application:

Protocol NamePort Number
Hypertext Transfer Protocol (HTTP)80
File Transfer Protocol (FTP)21
TELecommunication NETwork (Telnet)23
E-mail Protocol43
Network News Transfer Protocol (NNTP)119

Transport:
When you have to write Network Programs That is used to TCP (Transmission Control Protocol) or UDP (User Datagram Protocol). TCP: TCP provides a Point-to -Point Channel For applications that require reliable Communication. It is higher-level Protocol that Manages to robustly String together these Packets, Sorting and retransmitting them as necessary to reliably transmit data. UDP: UDP Sends independent packets of data, called datagrams, from one Application to other.
Network IP:
An IP (Internet Protocol) address describe a network hierarchy. All Internet address Consisted of 32-bit Values, Organized as four 8-bit values.This address Type was specified by IPv4 (Internet Protocol, Version 4) however, a new addressing scheme, called IPv6 (Internet Protocol, Version 6) has come in to play.IPv6 uses a 128-bit value to represent an address, organized into eight 16-bit chunks. Although there are several reasons for and advantages to IPv6, the main one is that it supports a much larger address space than does IPv4.
InetAddress:
The InetAddress class is used to encapsulate both the numerical IP address and the domain name for the address. InetAddress can handle both IPv4 and IPv6 address.
Factory Methods:
The InetAddress class has no visible constructors. To InetAddress object, you have to use one of the available factory methods.
There commonly used InetAddress factory methods are shown here.



The getLocalHost() method simply returns the InetAttress object that represents the localhost. The getByName() method returns an InetAddress for a host name passed to it. if these methods are unable to resolve the host name, they thow an UnknownHostException. getAllByName() factory method returns an array of InetAddress that represent all of the address that a particular name resolve to. It will also throws an UnknownHostException if it can't resolve the name to at least one address. InetAddress also includes the factory method getByAddress(), which takes an IP Address and returns an InetAddress object.
Example of First InetAddress :



Out Put:
                                                           Manoj-PC/100.118.88.245 
                                                           www.HerbSchildt.com/216.92.65.4 
                                                           www.nba.com/223.196.82.72 
                                                           www.nba.com/223.196.82.73