Network Programming (2) -- connect the parent class URLConnection and urlconnection

Source: Internet
Author: User

Network Programming (2) -- connect the parent class URLConnection and urlconnection
In network programming (1), we learned that if you want to access resources of other hosts, you need to know the URL of the resource. If we know the URL, how can we access resources on the host? This article focuses on the basic parent class URLConnectiion of Android Network Programming in Java, which uses URLs to access host resources, the features of each subclass further optimize and support the new network protocol, from comprehensive to local, from extensive to fine-grained system learning. You can see java.lang.Objectjava.net. URLConnection from JavaAPI

URLConnection is a super class of the network connection class. It represents the communication link between the application and the URL.
Its direct subclass is: HttpURLConnection, JarURLConnection
HttpURLConnection is a URLConnection that supports specific HTTP functions. This class is one of the most common network resource category classes.
JarConnection is also a class that can be used to connect Java documents or Jar files.
Such instances can be used to read and write resources referenced by this URL.
Generally, it takes several steps to connect to resources of other hosts:
1) Call the openConnection method on the URL to create a connection object.
<strong>URLConnection conn = url.openConnection();</strong>
Note: constructing a URL Connection to a specified URL does not create a connection to the object referenced by the URL. That is to say, it is only the connection between the two ends of the logical pipeline, but there is no read/write support.
2) set parameters and common request attributes.
For example, after we connect to the remote host, we need to set some read restrictions. For example, whether to use the cache or not allows the resource to be read.

Modify the parameters as follows:
    SetAllowUserInteraction (boolean allowuserinteraction)

    Whether user interaction is allowed. For example, the verification dialog box is started after access to the background.

      SetDoInput (boolean doinput)

      Set the input stream, that is, URL connections can be used for input and/or output. Set the doInput flag to true to indicate that the application needs to read data from the URL Connection. The default value is true.

        setOutInput(boolean dooutput)
        Set the output stream, that is, URL connections can be used for input and/or output. Set the doOutput flag to true to indicate that the application will write data to the URL Connection. The default value is flase.
         
         
          setUseCaches(boolean usecaches)
          Set whether to use Cache
             
          setConnectTimeout(int timeout)
          Sets the connection timeout time. A specified timeout value (in milliseconds) is set in milliseconds. This value is used to open the communication link of the resource referenced by this URLConnection. If the timeout period expires before the connection is established, a java.net. SocketTimeoutException is triggered. Zero timeout indicates an infinite timeout.

          setReadTimeout(int timeout)
          Set the read timeout value in milliseconds.

          SetContentHandlerFactory (ContentHandlerFactory fac)

          Set ContentHandlerFactory of the application. An application can call this method at most once. The ContentHandlerFactory instance is used to construct a content handler Based on the content type.
          Set Request properties using the following methods:
          setRequestProperty(String key,String value)
          Set Request attributes. (Common methods)
          3) Use connectMethod to establish the actual connection to the remote object.
          <span style="font-size:14px;"><span style="font-family:SimHei;"><span style="font-weight: normal;">Conn.connect();</span></span></span>

          Note: The URLConnection object goes through two phases: Create an object and establish a connection. STEP (1) is only the connection phase. You can set the preceding parameters and attributes between steps (1) and (3. 4) perform operations on the corresponding resources.
          After the connection, the remote object becomes available. The header field and content of the remote object become accessible.
          The header field can be understood as the configuration information file of the client request message and the server response message, which describes and limits the content.
          The content is the subject of interaction between the client and the server.
           
          
          Common 6-header field access methods:
          Public int getContentLength () returns the value of the content-length header field. Return Description: The content length of the resource referenced by the URL of the connection, or-1 is returned if the content length is unknown. Public String getContentType () returns the value of the content-type header field. Return Description: content type of the resource referenced by the URL, or null is returned if the type is unknown. Public String getContentEncoding () returns the value of the content-encoding header field. Return Description: Content Encoding of the resource referenced by the URL, or null is returned if the encoding is unknown. Public long getExpiration () returns the value of the expires header field. Return Description: expiration date of the resource referenced by this URL, or 0 is returned if it is unknown. This value is the number of milliseconds from January 1, January 1, 1970. public long getDate () returns the value of the date header field. Return Description: The sending date of the resource referenced by the URL, or 0 is returned if it is unknown. The returned value is the number of milliseconds from January 1, January 1, 1970 to Greenwich Mean Time. Public long getLastModified () returns the value of the last-modified header field. The result is the number of milliseconds from January 1, January 1, 1970 to Greenwich Mean Time. Return Description: The last modification date of the resource referenced by this URLConnection, or 0 if it is unknown. Public String getHeaderField (String name) (this method is used to obtain the corresponding header field value, in the form of a key value) to return the value of the specified header field. If the header field is set using different values for multiple called connections, only the last set value is returned. Parameter name-the name of the header field. Return Description: the value of the specified header field, or if the header does not contain such a field, null is returned. Public Map <String, List <String> getHeaderFields () returns an unchangeable Map of the header field. The Map key is a String that represents the name of the response-header field. Each Map value is a list of unmodifiable strings, which represent the corresponding field values. Description of returned results: The MapAPI of the header field also has

          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.