Java-urlconnection class explanation

Source: Internet
Author: User

Abstract class URLConnection is a superclass of all classes that represents a communication link between an application and a URL. Instances of this class can be used to read and write resources referenced by this URL. In general, creating a connection to a URL requires several steps:

OpenConnection ()    operates on parameters that affect the connection to a remote resource. Connect ()    interacts with the resource; Query header fields and content.

    1. Create a Connection object by calling the OpenConnection method on the URL.
    2. Action set parameters and general request properties.
    3. Use the Connect method to establish an actual connection to the remote object.
    4. The remote object becomes available. The header fields and contents of the remote object become accessible.
Importjava.net.*;ImportJava.io.*;Importjava.util.Date; Public classUrlconnectiondemo { Public Static voidMain (string[] args)throwsexception{//Urlconnectiondemo Urlconnectiondemo = new Urlconnectiondemo ();             intC; URL HP=NewURL ("http://www.hao123.com/"); URLConnection Hpcon=hp.openconnection (); System.out.println ("Date:" +NewDate (Hpcon.getdate ())); System.out.println ("Content-type:" +Hpcon.getcontenttype ()); System.out.println ("Expires:" +hpcon.getexpiration ()); System.out.println ("Last-modified:" +NewDate (Hpcon.getlastmodified ())); intlen=hpcon.getcontentlength (); System.out.println ("Content-length:" +Len); if(len>0) {System.out.println ("= = = Content = = ="); InputStream input=Hpcon.getinputstream (); intI=Len;  while((C=input.read ())!=-1 && (--i>0) {System.out.print (Char) c); }             }               ElseSystem.out.println ("No Content available!"); }               }

Operation Result:

Date:mon 11:07:02 CSTContent-type:text/1188443222000 last-modified:fri 2412 : 29:00 CSTcontent-length:44966=== content === ...

Java-urlconnection class explanation

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.