Java Network Programming Summary URLConnection protocol processor

Source: Internet
Author: User
Tags ftp site response code

URLs and URLConnection classes

URLs in the network (Uniform Resource Locator) are abbreviations for Uniform Resource locators. It represents the address of a resource on the Internet. Through the URL we can access various network resources on the Internet, such as the most common www,ftp site. URLs can be thought of as "pointers" to Internet resources, which can be used to obtain information about Internet resources, including information about the InputStream object acquiring the URL, and a connection urlconnection to the remote object referenced by the URL. The URLConnection object can send requests and read the URL's resources to the URL represented. Typically, creating a connection to a URL requires the following steps:

    1. Create the URL object and obtain the URLConnection object by calling the OpenConnection method;
    2. Set URLConnection parameters and normal request properties;
    3. Send a request to a remote resource;
    4. When a remote resource becomes available, the program can access the remote resource's header field and read the information returned by the remote resource through the input stream.

Here we need to focus on the third step: if you just send a Get method request, use the Connect method to establish a connection to a remote resource, and if it is a request to send a post, you need to get the output stream corresponding to the URLConnection object to send the request. It is important to note that since the parameter of the Get method is passed by explicitly appending the parameter to the address, the parameter when constructing the URL object should be the full URL address containing the parameter, and after the URLConnection object has been obtained, You can simply call the Connect method to send the request. The Post method only requires a page URL when passing parameters, and the parameters are passed through the output stream as needed. Also, you need to set the header field.

URLConnection is a class in a protocol processor that represents an active connection to a resource specified by a URL. Mainly used in two aspects, one is the interaction with the server (especially the HTTP server), can be used to view the server sends the header, set the properties of the connection, set the client's request header and so on. It can also be used to implement post and put methods to send data. Another aspect is part of the Java Protocol processor mechanism. The so-called protocol handler separates the details of the processing protocol from the processing of a particular data type and involves client-server interaction, such as generating the correct request format, explaining the header returned with the data, and so on.


Get URLConnection
Based on an already created URL to open a urlconnection by openconnection, although the URL to obtain, in fact, the internal call is URLStreamHandler OpenConnection, The method is an abstract method that returns a URLConnection that is returned based on the type of protocol and returns a httpurlconnection if it is an HTTP URL.
URL url=new url ("http://www.baidu.com");
URLConnection uc=url.openconnection ();
At this point the UC is not connected, the local and remote host is unable to send and receive data, you need to call the Uc.connect () method to establish a connection. However, this method is called automatically when other requirements such as getInputStream (), Getheaderfield () are used.
With this urlconnection it is easy to read data from the server side as long as its getinputstream.


The difference between URL and Urlconnectin
URLConnection provides access to HTTP headers
URLConnection can configure the request parameters that the client sends to the server is the header
URLConnection can interact with the server using request methods such as Post,put


Read the header of the server response
For the header of the server response, it is, of course, based on the HTTP server response, which can be used to get the header field in the response.
Content-type,content-length,content-encoding,date,last-modified,expires
You can use getContentType () to obtain the MIME type of the data, determine the character encoding, and display it in a normal format, such as

String encoding= "Iso-8859-1";//http Default encoding methodURL u=Newurl (URL); URLConnection UC=u.openconnection (); String type=Uc.getcontenttype (); //get the character encoding method  intStar=type.indexof ("charset="); if(Star!=-1) Encoding=type.substring (star+8); System.out.println ("CharSet:" +encoding); InputStream Raw=Uc.getinputstream (); Reader R=NewInputStreamReader (NewBufferedinputstream (Raw), encoding);

Use Getcontentlength to get the binary file size to download the file

URLConnection uc=url.openconnection (); String ContentType=Uc.getcontenttype (); intContentlength=uc.getcontentlength (); if(Contenttype.startswith ("text/") | | Contentlength==-1)    {      Throw NewIOException ("This was not a binary file"); } InputStream Raw=NewBufferedinputstream (Uc.getinputstream ()); byte[] Data=New byte[ContentLength]; intBytesread=0; intOffset=0;  while(offset<contentlength) {Bytesread=raw.read (data, offset, contentlength); if(Bytesread==-1) Break; Offset+=Bytesread;        } raw.close (); if(offset!=contentlength) {      Throw NewIOException ("only read" +offset+ "bytes; Expected "+contentlength+" bytes "); }         //gets the stream to be written to the local, based on the name of the file path in the URL.String file=Url.getfile (); intStart=file.lastindexof ("/"); String filename=file.substring (start+1); FileOutputStream Fout=Newfileoutputstream (filename);    Fout.write (data);    Fout.flush (); Fout.close ();

Getheaderfield (String name), which can get the value of the name in case of a given header name.
Getheaderfieldkey (int n) and getheaderfield (int n) return the names and values in the header, which is useful

URL u=new  url (URL); URLConnection UC=u.openconnection ();  for (int. j=1;; J + +) {  String header=Uc.getheaderfield (j  ); if (header==null) break; // Skip the Loop  System.out.println (Uc.getheaderfieldkey (j) + ":" +header);}

Configure the connection
The so-called configuration basically defines how the client makes a request to the server.
The general client, by default, Doinput to True, indicating that data sent from the server can be accepted, which must be set before the URLConnection connection. If you want to interact with a post and put, you must set DOOUTPU to True, which is false by default


Configuring the HTPP header sent by the client
When a client makes server access, some protocols need to be preceded by a header, some name value pairs are configured, and can be set by Setrequestproperty (Name,value), and multiple values are separated by commas.


The client uses post to send data

 Packagecom.urlconnection;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter;ImportJava.io.Writer;ImportJava.net.URL;Importjava.net.URLConnection;//simulate form submission processing Public classFormposter {PrivateURL url; PrivateQueryString query=NewQueryString (); //must be guaranteed to be the HTTP protocol   Publicformposter (url url) {if(!url.getprotocol (). toUpperCase (). StartsWith ("HTTP"))              {             Throw NewIllegalArgumentException ("Posting works for HTTP URLs"); }             This. url=URL; }     Public voidAdd (String name,string value) {query.add (name, value); }     PublicURL GetURL () {return  This. URL; }     PublicInputStream Post ()throwsIOException {urlconnection UC=url.openconnection (); Uc.setdooutput (true); Writer out=NewOutputStreamWriter (Uc.getoutputstream (), "ASCII"); //post lines, Content-type and Content-length are sent by URLConnection//just send data toOut.write (query.tostring ()); Out.write ("\r\n\r\n");    Out.flush ();        Out.close (); returnUc.getinputstream (); }     Public Static voidMain (string[] args) {//TODO auto-generated Method StubString u= "Http://www.baidu.com"; URL URL=NULL; Try{URL=NewURL (U); }Catch(IOException e) {} formposter poster=Newformposter (URL); Poster.add ("Hawk", "FDAFDA"); Poster.add ("Good Morning", "Fdafa"); Try{InputStream in=Poster.post (); //Read ResponseInputStreamReader r=NewInputStreamReader (in); intC;  while((C=r.read ())!=-1) {System.out.print (Char) c);                 } in.close (); }Catch(IOException ex) {System.err.println (ex); }  }}

HttpURLConnection
This is a special operation of the HTTP URL class, inheriting the child urlconnection, there are 7 main methods of request,
Use Setrequestmethod (String method) to set different request methods, default is GET, case-sensitive
Get, get data from server side
POST, submit the form to the server side
PUT, upload files to server
Head, get the server-side response header
OPTIONS, which query server supports which request methods
Delete to remove files from the server
TRACE, the server returns the HTTP header sent by the client to detect how the Proxy server modifies the HTTP header
The format of the server response is as follows:
http/1.1 OK
...
This class adds two methods
Getresponsemessage () Gets the message corresponding to the response code, such as OK
Getresponsecode () Gets the response code, such as 200

The


Protocol processor
    Protocol processor is mainly based on the protocol in the URL to find the appropriate protocol processor for client-server interaction. The main concerns are four classes, specific class URLs, abstract classes URLConnection and URLStreamHandler, and interface urlstreamhandlerfactory. The protocol processor's stream processor always finds the most appropriate URLConnection
based on the specified protocol     to establish the Protocol processor itself, You need to write two subclasses of URLConnection and URLStreamHandler, and then create a urlstreamhandlerfactory. The
    URLConnection subclass mainly handles interacting with the server, converting the data sent by the server to InputStream, and converting all data sent by the client to OutputStream. The URLStreamHandler subclass parses the string representation of a URL into parts, sets the parts of the URL object, and creates a urlconnection that understands the sub-URL protocol.

The basic flow of the


Protocol processor is as follows: The
1 program first constructs a URL object for a protocol using a string, and in the Create URL pattern, only verifies that the URL pattern is recognized, without checking the correctness of its format
2 The constructor uses the passed arguments to determine the protocol portion of the URL, such as the HTTP
3 URL () constructor, which attempts to find the given URLStreamHandler
   A If you have used this protocol before, Get URLStreamHandler
   b from Cache Otherwise, if URLStreamHandlerFactory is set, pass this string to factory www.it165.net
   C If none of the two are, try instantiating a            that is listed in the Java.protocol.handler.pkgs property         protocol. Handler URLStreamHandler
   D If the instantiation fails, try instantiating a protocol.handler   in Sun.net.www.protocol           URLStreamHandler
   e If one of them succeeds, Set the property field Handler field. If all is not successful, then throw                          Malformedurlexception Exception
4 program calls the OpenConnection method of the URL object
5 The URL allows URLStreamHandler to return a urlconnection
6 that is appropriate for this URL using URLConnection to interact with remote resources


URLStreamHandler the process of parsing a field in a URL
URL (String)-->url (url,string)-->url (url,string,string)-->urlstreamhandler.parseurl ()
-->urlstreamhandler.seturl ()-->url.set ();
To create a new urlstreamhandler generally only need to modify the OpenConnection method, as needed to modify parseURL
For each of the URLConnection subclasses, rewrite a Connect method, which contains the specific steps to establish the socket.
Then use URLStreamHandlerFactory to register these stream processors

Java Network Programming Summary URLConnection protocol processor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.