趙雅智_java 網絡編程(4)之URL – JAVA編程語言程序開發技術文章

讓IE作為客戶端編寫服務端程序:

[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("訪問成功"); 
        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("訪問成功");
  s.close();
  //ss.close();
 }

}

 

connect 套接字:

客戶端:

[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); 
            //等於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);
   //等於Socket s = new Socket("192.168.49.58",9001));
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

}

服務端

[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:

類 URL 代表一個統一資源定位符,它是指向互聯網“資源”的指針。資源可以是簡單的文件或目錄,也可以是對更為復雜的對象的引用

常用的兩個構造方法:


URL(String spec) :根據 String 表示形式創建 URL 對象。
URL(URL context, String spec): 通過在指定的上下文中對給定的 spec 進行解析創建 URL。

例:

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

相對URL對象,一般用在HTML文件中,例如:

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

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

URL urlbaidu_b = new URL(urlbaidu,"b.html");

訪問URL對應的資源常用的調用方法:

String getFile() : 獲取此 URL 的文件名。
String getHost() :獲取此 URL 的主機名(如果適用)。
String getPath() :獲取此 URL 的路徑部分。
int getPort() :獲取此 URL 的端口號。
String getProtocol() :獲取此 URL 的協議名稱。
String getQuery() :獲取此 URL 的查詢部分。
URLConnection openConnection() :返回一個 URLConnection 對象,它表示到 URL 所引用的遠程對象的連接。
InputStream openStream() :打開到此 URL 的連接並返回一個用於從該連接讀入的 InputStream。

[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();
  }
  
 }

}

 

通過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();
 }
}

 

java地址欄裡的亂碼轉成普通字符:

 

URLDecoder類中有一個decode(String s, String enc)的靜態方法:將亂碼的特殊字符轉成普通字符

URLEncoder類中有一個encode(String s, String enc)的靜態方法:將普通字符轉成亂碼的特殊字符

[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程序開發","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程序開發","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();
 }
 
 
 
 }

}

 

發佈留言