java 网络编程基础 1

关于协议:

应用层网络协议包括:http,ftp,telnet,.....

传送层网络协议:使用socket封装的TCP,或UDP

端口:

用于网络通讯时传输数据时区分当前网络连接是与本机中的哪个应用程序交互的代号。

这个代号在传输数据时会存在数据报中。

java中哪些网络请求类使用TCP封装:

   URLURLConnectionSocket, and ServerSocket

java中哪些网络请求类使用UDP封装:

  DatagramPacketDatagramSocket, and MulticastSocket 

URL:访问万维网信息的连接:包含两个部分:协议,地址。

  • 协议:  http://example.com,  http 是协议.
  • 地址:  http://example.com,  example.com 是地址.

java中的URL类初始化:

   1、  URL aUrl = new URL("http","example.com","/pages/page1.html");

 2、  URL aUrl = new URL(http://example.com/pages/page1.html");

   3、  URL gamelan = new URL("http", "example.com", 80, "pages/page1.html");

 4、包含特殊字符的URL如:http://example.com/hello world/  hello world中又一个空格字符

        这时需要将空格字符转义成合法的URL字符转义后的结果

   URL url = new URL("http://example.com/hello%20world");

  如何快速转义java URL中的特殊字符? 使用URI类:

  URI uri = new URI("http", "example.com", "/hello world/", "");

  URL url = uri.toURL();

  

  构建URL过程使用的Exception类型:MalformedURLException

  URL类中的get 方法s 得到的结果:

基于URL http://example.com:80/docs/books/tutorial/index.html?name=networking#DOWNLOADING URL类中get方法获得的值

getProtocol(Returns the protocol identifier component of the URL.)

  http

getAuthorityReturns the authority component of the URL.

example.com:80

getHostReturns the host name component of the URL.

example.com

getPortReturns the port number component of the URL. The getPort method returns an integer that is the port number. If the port is not set, getPort returns -1.

80

getPathReturns the path component of this URL.

/docs/books/tutorial/index.html

getQueryReturns the query component of this URL.

name=networking

getFileReturns the filename component of the URL. The getFile method returns the same as getPath, plus the concatenation of the value of getQuery, if any.

/docs/books/tutorial/index.html?name=networking

getRefReturns the reference component of the URL.

DOWNLOADING
原文地址:https://www.cnblogs.com/codetime/p/5216400.html