Java provides our network support with a java.net package that allows us to programmatically access the Web Services features, this blog, to share with you. Knowledge of network programming in Java. The main lesson is to learn the API under the Java.net package.
Uri-->url
URI: Represents a Uniform resource identity, which is a simple string that identifies a resource in a uniform way.
Such a string begins with a scheme.
URL: A task that represents a uniform resource locator, and a URL is a URI.
URN: Generally, you cannot provide a persistent name for a resource. This is a unified resource-naming task. A urn is also a URI, but it is the only, persistent, world-wide. Even if the resource no longer exists or is no longer used.
URL class
The URL class represents a Uniform resource locator, and the resource can be a simple folder. Can also be complex objects, such as queries against a database or search engine.
The URL class gives us a different way to construct
1. Construct URLs based on address
Public URL (String spec), —————————————— URL url=new url ("www.baidu.com")
2. Based on protocol, hostname, port and file creation
Public URL (String protocol,string host,int port,string file)
URL encapsulates object information
The URL object encapsulates information about the object, such as the port number of the URL object, the header content, the hostname, the protocol name, and so on.
/** * @FileName: Urltest.java * @Package: Com.socket * @Description: TODO * @author: LUCKY * @date: December 11, 2015 Morning 8:48:34 * @v Ersion V1.0 */package com.socket;import java.awt.image.imageproducer;import java.net.url;/** * @ClassName: URLTest * @Description: TODO * @author: LUCKY * @date: December 11, 2015 morning 8:48:34 */public class Urltest {public static void main (string[] args) {try {//create URL url=new URL by address ("http://ww2.sinaimg.cn/large/70c8c5b7gw1erdli8ka8vj21360m1ju9.jpg");// Access to Information System.out.println (url.getauthority ()); System.out.println (Url.getpath ()); System.out.println (Url.getport ()); System.out.println (Url.getdefaultport ()); System.out.println (Url.getfile ()); System.out.println (Url.getprotocol ()); Object o=url.getcontent (); if (o instanceof imageproducer) {ImageProducer i= ( ImageProducer) O; System.out.println (i);}} catch (Exception e) {//Todo:handle Exception}}}
URLConnection Web Connection class
After you have the URL. How to connect with the Web page. At this time, we need to urlconnection class to serve us.
It represents a communication connection between the application and the URL.
The following steps are typically required to create a Rul connection.
1. Create a linked object by using the OpenConnection () method on the URL.
2. Setting general request properties for request parameters
Frequently used properties have the ability to perform user interaction, be able to input output, enable caching, and so on
3. Use the Connect () method to establish an actual connection to a remote object
4. After establishing a connection to a remote object, get access to the fields and content
For example, get the type of file, the time the file was created, the expiration time of the file, the length of the file, etc.
5. Closing the connection is required
/** * @FileName: Urlconnectiontest.java * @Package: Com.socket * @Description: TODO * @author: LUCKY * @da te:2015 December 11 Morning 8:54:24 * @version V1.0 */package com.socket;import java.io.bufferedoutputstream;import Java.io.F Ileoutputstream;import Java.io.inputstream;import Java.net.url;import Java.net.urlconnection;import com.sun.org.apache.xerces.internal.util.uri.malformeduriexception;/** * @ClassName: Urlconnectiontest * @ Description:todo * @author: LUCKY * @date: December 11, 2015 morning 8:54:24 */public class Urlconnectiontest {public static void Main (string[] args) throws Exception {try {//create Urlurl url=new URL by address ("http://ww2.sinaimg.cn/large/ 70c8c5b7gw1erdli8ka8vj21360m1ju9.jpg ");//Get Urlconnecionurlconnection connection=url.openconnection ();// Open connection Connection.connect ();//Get Attribute System.out.println (Connection.getcontentlength ()); System.out.println (Connection.getcontenttype ()); System.out.println (Connection.getdate ()); System.out.println (Connection.getexpiration ());System.out.println (Connection.getlastmodified ());//Read page data int c;inputstream is=connection.getinputstream (); Bufferedoutputstream out = new Bufferedoutputstream (New FileOutputStream ("Copy.png")), while ((C=is.read ())!=-1) { Out.write (c); System.out.println ((char) c);} Out.flush (); Is.close ();} catch (Malformeduriexception e) {//Todo:handle exceptione.printstacktrace ();}}}
Network programming based on HTTP protocol in Java