Zhao yazhi _ java Network Programming (4) URL

Source: Internet
Author: User
Tags connect socket

Let IE write the server program as the client:

[Java]
Package net. csdn. web;
 
Import java. io. IOException;
Import java. io. PrintWriter;
Import java.net. ServerSocket;
Import java.net. Socket;
 
Public class ServerDemo {
 
/**
* @ Param args
* @ Throws IOException
*/
Public static void main (String [] args) throws IOException {
// TODO Auto-generated method stub
ServerSocket ss = new ServerSocket (9009 );
Socket s = ss. accept ();
System. out. println (s. getInetAddress (). getHostAddress () + "...... conection ");
PrintWriter pwout = new PrintWriter (s. getOutputStream (), true );
Pwout. println ("Access successful ");
S. close ();
// Ss. close ();
}
 
}

Package net. csdn. web;

Import java. io. IOException;
Import java. io. PrintWriter;
Import java.net. ServerSocket;
Import java.net. Socket;

Public class ServerDemo {

/**
* @ Param args
* @ Throws IOException
*/
Public static void main (String [] args) throws IOException {
// TODO Auto-generated method stub
ServerSocket ss = new ServerSocket (9009 );
Socket s = ss. accept ();
System. out. println (s. getInetAddress (). getHostAddress () + "...... conection ");
PrintWriter pwout = new PrintWriter (s. getOutputStream (), true );
Pwout. println ("Access successful ");
S. close ();
// Ss. close ();
}

}

 

Connect socket:

Client:


[Java]
Package net. csdn. web;
 
Import java. io. IOException;
Import java.net. InetSocketAddress;
Import java.net. Socket;
 
Public class ConnectClient {
 
Public static void main (String [] args ){
Try {
Socket s = new Socket ();
InetSocketAddress isa = new InetSocketAddress ("192.168.49.58", 9001 );
S. connect (isa, 5000 );
// Equal to Socket s = new Socket ("192.168.49.58", 9001 ));
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
 
}

Package net. csdn. web;

Import java. io. IOException;
Import java.net. InetSocketAddress;
Import java.net. Socket;

Public class ConnectClient {

Public static void main (String [] args ){
Try {
Socket s = new Socket ();
InetSocketAddress isa = new InetSocketAddress ("192.168.49.58", 9001 );
S. connect (isa, 5000 );
// Equal to Socket s = new Socket ("192.168.49.58", 9001 ));
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}

}

Server


[Java]
Package net. csdn. web;
 
Import java. io. IOException;
Import java.net. ServerSocket;
Import java.net. Socket;
 
Public class ConnectServer {
Public static void main (String [] args) throws IOException {
 
ServerSocket ss = new ServerSocket (9002 );
Socket s = ss. accept ();
System. out. println (s. getInetAddress (). getHostAddress () + "...... conection ");

}
 
}

Package net. csdn. web;

Import java. io. IOException;
Import java.net. ServerSocket;
Import java.net. Socket;

Public class ConnectServer {
Public static void main (String [] args) throws IOException {

ServerSocket ss = new ServerSocket (9002 );
Socket s = ss. accept ();
System. out. println (s. getInetAddress (). getHostAddress () + "...... conection ");

}

}

 

 

 

URL:

The class URL represents a uniform resource locator, which is a pointer to the Internet "resource. Resources can be simple files or directories, or references to more complex objects.

Two common constructor methods:


URL (String spec): Creates a URL object based on the String representation.
URL (URL context, String spec): Creates a URL by parsing the specified spec in the specified context.

Example:

URL url = new URL ("http://www.baidu.com ");

Relative URL objects are generally used in HTML files, for example:

URL urlbaidu = new URL ("http://www.baidu.com ");


URL urlbaidu_a = new URL (urlbaidu, "a.html ");

URL urlbaidu_ B = new URL (urlbaidu, "B .html ");

Common call methods for accessing resources corresponding to a URL:


String getFile (): get the name of the URL file.
String getHost (): Obtain the Host Name of the URL (if applicable ).
String getPath (): Obtain the path of this URL.
Int getPort (): gets the port number of this URL.
String getProtocol (): gets the protocol name of this URL.
String getQuery (): Obtain the query part of this URL.
URLConnection openConnection (): returns a URLConnection object, which indicates the connection to the remote object referenced by the URL.
InputStream openStream (): Open the connection to this URL and return an InputStream used to read from this connection.

[Java]
Package net. csdn. web;
 
Import java.net. MalformedURLException;
Import java.net. URL;
 
Public class UrlDemo {
 
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
 
Try {
URL url = new URL ("http://dianying.taobao.com /? Spm = 1.1000386.221827.9 ");
System. out. println ("protocol =" + url. getProtocol ());
System. out. println ("authority =" + url. getAuthority ());
System. out. println ("host =" + url. getHost ());
System. out. println ("port =" + url. getPort ());
System. out. println ("path =" + url. getPath ());
System. out. println ("query =" + url. getQuery ());
System. out. println ("filename =" + url. getFile ());
System. out. println ("ref =" + url. getRef ());
} Catch (MalformedURLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}

}
 
}

Package net. csdn. web;

Import java.net. MalformedURLException;
Import java.net. URL;

Public class UrlDemo {

/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub

Try {
URL url = new URL ("http://dianying.taobao.com /? Spm = 1.1000386.221827.9 ");
System. out. println ("protocol =" + url. getProtocol ());
System. out. println ("authority =" + url. getAuthority ());
System. out. println ("host =" + url. getHost ());
System. out. println ("port =" + url. getPort ());
System. out. println ("path =" + url. getPath ());
System. out. println ("query =" + url. getQuery ());
System. out. println ("filename =" + url. getFile ());
System. out. println ("ref =" + url. getRef ());
} Catch (MalformedURLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}

}

}

 


Download webpage information through URL


[Java]
Package net. csdn. web;
 
Import java. io. BufferedReader;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java.net. URL;
 
Public class TestNet3 {
Public static void main (String [] args) throws IOException {
URL url = new URL ("http://www.baidu.com /");
InputStreamReader isr = new InputStreamReader (url. openStream ());
BufferedReader in = new BufferedReader (isr );
String inputLine;
FileOutputStream fos = new FileOutputStream ("d: \ abc.html ");

While (inputLine = in. readLine ())! = Null ){
Fos. write (inputLine. getBytes ());
System. out. println (inputLine );
}
In. close ();
}
}

Package net. csdn. web;

Import java. io. BufferedReader;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java.net. URL;

Public class TestNet3 {
Public static void main (String [] args) throws IOException {
URL url = new URL ("http://www.baidu.com /");
InputStreamReader isr = new InputStreamReader (url. openStream ());
BufferedReader in = new BufferedReader (isr );
String inputLine;
FileOutputStream fos = new FileOutputStream ("d: \ abc.html ");

While (inputLine = in. readLine ())! = Null ){
Fos. write (inputLine. getBytes ());
System. out. println (inputLine );
}
In. close ();
}
}

 

The garbled characters in the java address bar are converted to common characters:

 


The URLDecoder class has a static method of decode (String s, String enc): Convert garbled special characters into common characters.


The URLEncoder class has a static method of encode (String s, String enc): Convert common characters into special characters with garbled characters.


[Java]
Package net. csdn. web;
 
Import java. io. UnsupportedEncodingException;
Import java.net. URLDecoder;
Import java.net. URLEncoder;
 
Public class TestNet4 {
 
/**
* @ Param args
*/
Public static void main (String [] args ){

Try {
String str1 = URLEncoder. encode ("java program development", "UTF-8 ");
System. out. println (str1 );

String str2 = URLDecoder. decode ("java % E7 % A8 % 8B % E5 % BA % 8F % E5 % BC % 80% E5 % 8F % 91", "UTF-8 ");
System. out. println (str2 );
} Catch (UnsupportedEncodingException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}



}
 
}

Package net. csdn. web;

Import java. io. UnsupportedEncodingException;
Import java.net. URLDecoder;
Import java.net. URLEncoder;

Public class TestNet4 {

/**
* @ Param args
*/
Public static void main (String [] args ){
 
Try {
String str1 = URLEncoder. encode ("java program development", "UTF-8 ");
System. out. println (str1 );

String str2 = URLDecoder. decode ("java % E7 % A8 % 8B % E5 % BA % 8F % E5 % BC % 80% E5 % 8F % 91", "UTF-8 ");
System. out. println (str2 );
} Catch (UnsupportedEncodingException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
 
 
 
}

}


 

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.