網路編程4--畢向東java基礎教程視頻學習筆記,4--畢向東
Day24
06 自訂瀏覽器-Tomcat服務端
07 自訂圖形介面瀏覽器-Tomcat服務端
08 URL-URLConnection
09 小知識點
10 網域名稱解析
06 自訂瀏覽器-Tomcat服務端
用戶端:自訂
服務端:Tomcat
1 import java.net.*; 2 import java.io.*; 3 class MyIE 4 { 5 public static void main(String[] args)throws Exception 6 { 7 Socket s=new Socket("127.0.0.1",8080); 8 PrintWriter out=new PrintWriter(s,getOutputStream(),true); 9 10 out.prinln("GET /myWeb/Demo.html HTTP/1.1");11 out.prinln("Accept:*/*");12 out.println("Accept-Language:zh-cn");13 out.println("Host:127.0.0.1:11000");14 out.println("Connection:closed");15 16 out.println();17 out.println();18 19 BufferedReader bufr=20 new BufferedReader(new InputStreamReader(s.getInputStreamReader));21 String line=null;22 while((line=bufr.readLine())!=null)23 {24 System.out.println(line);25 }26 s.close();27 }28 }View Code
在Tomcat服務端開啟的情況下,運行此程式控制台便能顯示服務端發給用戶端的資訊。
07 自訂圖形介面瀏覽器-Tomcat服務端
實現圖形化介面的瀏覽器代碼如下,程式的關鍵是對輸入的網址進行分割,獲得ip地址,連接埠號碼和訪問路徑。
1 import java.awt.*; 2 import java.awt.event.*; 3 import java.io.*; 4 import java.net.*; 5 class MyIEByGUI 6 { 7 private Frame f; 8 private TextField tf; 9 private Button but; 10 private TextArea ta; 11 12 private Dialog d; 13 private Label lab; 14 private Button okBut; 15 16 17 MyIEByGUI() 18 { 19 init(); 20 } 21 public void init() 22 { 23 f = new Frame("my window"); 24 f.setBounds(300,100,600,500); 25 f.setLayout(new FlowLayout()); 26 27 tf = new TextField(60); 28 29 but = new Button("轉到"); 30 31 ta = new TextArea(25,70); 32 33 34 d = new Dialog(f,"提示資訊-self",true); 35 d.setBounds(400,200,240,150); 36 d.setLayout(new FlowLayout()); 37 lab = new Label(); 38 okBut = new Button("確定"); 39 40 d.add(lab); 41 d.add(okBut); 42 43 44 45 f.add(tf); 46 f.add(but); 47 f.add(ta); 48 49 50 myEvent(); 51 f.setVisible(true); 52 } 53 private void myEvent() 54 { 55 56 okBut.addActionListener(new ActionListener() 57 { 58 public void actionPerformed(ActionEvent e) 59 { 60 d.setVisible(false); 61 } 62 }); 63 d.addWindowListener(new WindowAdapter() 64 { 65 public void windowClosing(WindowEvent e) 66 { 67 d.setVisible(false); 68 } 69 }); 70 71 tf.addKeyListener(new KeyAdapter() 72 { 73 public void keyPressed(KeyEvent e) 74 { 75 try 76 { 77 if(e.getKeyCode()==KeyEvent.VK_ENTER) 78 showDir(); 79 } 80 catch (Exception ex) 81 { 82 } 83 84 } 85 }); 86 87 88 but.addActionListener(new ActionListener() 89 { 90 public void actionPerformed(ActionEvent e) 91 { 92 try 93 { 94 showDir(); 95 } 96 catch (Exception ex) 97 { 98 } 99 100 101 }102 });103 104 f.addWindowListener(new WindowAdapter()105 {106 public void windowClosing(WindowEvent e)107 {108 System.exit(0); 109 }110 });111 }112 113 private void showDir()throws Exception114 {115 116 ta.setText("");117 String url = tf.getText();//http://192.168.1.254:8080/myweb/demo.html118 119 int index1 = url.indexOf("//")+2;120 121 int index2 = url.indexOf("/",index1);122 123 124 125 String str = url.substring(index1,index2);126 String[] arr = str.split(":");127 String host = arr[0];128 int port = Integer.parseInt(arr[1]);129 130 String path = url.substring(index2);131 //ta.setText(str+"...."+path);132 133 134 Socket s = new Socket(host,port);135 136 PrintWriter out = new PrintWriter(s.getOutputStream(),true);137 138 out.println("GET "+path+" HTTP/1.1");139 out.println("Accept: */*");140 out.println("Accept-Language: zh-cn");141 out.println("Host: 127.0.0.1:11000");142 out.println("Connection: closed");143 144 out.println();145 out.println();146 147 BufferedReader bufr = new BufferedReader(new InputStreamReader(s.getInputStream()));148 149 String line = null;150 151 while((line=bufr.readLine())!=null)152 {153 ta.append(line+"\r\n");154 }155 156 s.close();157 158 }159 160 public static void main(String[] args) 161 {162 new MyIEByGUI();163 }164 }View Code
在Tomcat服務端開啟的情況下,在視窗上方的輸入網址區輸入:http://127.0.0.1:8080/myWeb/Demo.html,再按Enter或者轉到,運行顯示如下:
08 URL-URLConnection
在07中為了獲得有用資訊,分割字串很是麻煩。我們可以使用java.net包中的URL類。
類 URL 代表一個統一資源定位器,它是指向互連網“資源”的指標。
URL 可選擇指定一個“連接埠”,它是用於建立到遠程主機 TCP 串連的連接埠號碼。
如果未指定該連接埠號碼,則使用協議預設的連接埠。例如,http 協議的預設連接埠為 80。
構造方法:
URL(String spec):
URL(String protocl,String host;int port,String file)
1 import java.net.*; 2 class URLDemo 3 { 4 public static void main(String[] args)throws MalformedURLException 5 { 6 URL url=new URL("http://127.0.0.1:8080/myWeb/Demo.html?name=hahaha&name=20"); 7 8 System.out.println("getFile(): "+url.getFile()); 9 System.out.println("getHost(): "+url.getHost());10 System.out.println("getPath(): "+url.getPath());11 System.out.println("getPort(): "+url.getPort());12 System.out.println("getProtocol(): "+url.getProtocol());13 System.out.println("getQuery(): "+url.getQuery());14 }15 }16 /*17 String getFile() 18 擷取此 URL 的檔案名稱。 19 String getHost() 20 擷取此 URL 的主機名稱(如果適用)。 21 String getPath() 22 擷取此 URL 的路徑部分。 23 int getPort() 24 擷取此 URL 的連接埠號碼。 25 String getProtocol() 26 擷取此 URL 的協議名稱。 27 String getQuery() 28 擷取此 URL 的查詢部分。 29 30 運行結果:31 32 D:\abc>java URLDemo33 getFile(): /myWeb/Demo.html?name=hahaha&name=2034 getHost(): 127.0.0.135 getPath(): /myWeb/Demo.html36 getPort(): 808037 getProtocol(): http38 getQuery(): name=hahaha&name=2039 40 41 42 */View Code 1 import java.io.*; 2 import java.net.*; 3 class URLConnectionDemo 4 { 5 public static void main(String[] args) throws Exception 6 { 7 URL url=new URL("http://127.0.0.1:8080/myWeb/Demo.html"); 8 9 URLConnection conn=url.openConnection();10 System.out.println(conn);11 12 InputStream in=conn.getInputStream();13 byte[] buf=new byte[1024];14 int len =in.read(buf);15 System.out.println(new String(buf,0,len));16 }17 }View Code
09 小知識點
1.Socket類中有一個沒有參數的構造方法,這個構造方法一般和connect方法一起使用。
void connect(SocketAddress endpoint)
connect方法的參數類型SocketAddress類是一個抽象類別,它的直接子類InetSocketAddress和InetAddress類的區別就是:
InetAddress類中只封裝ip地址,而InetSocketAddress封裝了ip地址和連接埠號碼。
2.ServerSocket類有一個構造方法:ServerSocket(int port,int backlog),
其中backlog代表對串連請求的最大隊列長度,即此伺服器最多能串連的使用者個數。
10 網域名稱解析
1.因為ip地址不容易記憶,上網時常用主機名稱。
主機名稱翻譯成ip地址,則需要網域名稱解析(DNS)。比如上網時輸入www.sina.com.cn,瀏覽器就先向公網請求DNS服務,
把主機名稱翻譯成相應的ip地址,再把這個地址發回給瀏覽器。
2.http://127.0.0.1:8080/
http://localhost:8080/
上面這兩個地址是等價的,其實127和localhost的映射關係就在本機。
在C:\Windows\System32\drivers\etc路徑下的host檔案,檔案中有:
# localhost name resolution is handled within DNS itself.
#127.0.0.1 localhost
#::1 localhost
也可以修改檔案讓127.0.0.1對應別的名字。
上網時,先在本機找映射關係,找不到再用DNS。
可以利用這個特點避免惡意網站的侵擾。在此檔案中把一些惡意網站同127.0.0.1建立映射關係就可以了。