国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

Java網(wǎng)絡(luò)編程之InetAddress和URL

2019-11-14 23:52:56
字體:
供稿:網(wǎng)友
java網(wǎng)絡(luò)編程之InetAddress和URL Posted on 2015-04-20 22:34 墨香一點足以 閱讀(...) 評論(...) 編輯 收藏

在Java中提供了專門的網(wǎng)絡(luò)開發(fā)程序包---java.net,java的網(wǎng)絡(luò)編程提供了兩種通信協(xié)議:TCP(傳輸控制協(xié)議)和UDP(數(shù)據(jù)報協(xié)議)。

一.ip(Internet PRotocol) 與InetAddress類

1.IP簡介

互聯(lián)網(wǎng)上的每一臺計算機都有一個唯一表示自己的標識,即IP地址。

IP地址=網(wǎng)絡(luò)地址+主機地址

2.InetAddress

該類主要表示IP地址,有兩個子類:Inet4Address、Inet6Address,前者表示IPV4,后者表示IPV6。

InetAddress類的常用方法有:

類型 方法 描述
static InetAddress getByName(String host) 在給定主機名的情況下確定主機的 IP 地址。
static InetAddress

getLocalHost()

返回本地主機。
String getHostName() 獲取此 IP 地址的主機名。
boolean isReachable(int timeout) 測試是否可以達到該地址。
測試InetAddress類:
package org.demo.net;import java.net.InetAddress;/** * 測試InetAddress類 * @author oushine */public class InetAddressDemo {    public static void main(String[] args) {        try {            //聲明并得到本地InetAddress對象            InetAddress iAddress1=InetAddress.getLocalHost();            //聲明并得到遠程InetAddress對象            InetAddress iAddress2=InetAddress.getByName("www.baidu.com");            //獲得本地IP地址            System.out.println("本機IP地址為:"+iAddress1.getHostAddress());            //獲得遠程IP地址            System.out.println("百度的IP地址是:"+iAddress2.getHostAddress());            System.out.println("本機是否可達:"+iAddress1.isReachable(3000));        } catch (Exception e) {            e.printStackTrace();        }    }}

結(jié)果:

1111

二.URL與URLConnection

1.URL

URL(Uniform Resource Locator)是統(tǒng)一資源定位符,可以直接使用此類找到互聯(lián)網(wǎng)上的資源(比如一個網(wǎng)頁)。

URL類常用方法:

類型方法描述
構(gòu)造方法

URL(String spec)

根據(jù) String 表示形式創(chuàng)建 URL 對象。
構(gòu)造方法URL(String protocol, String host, int port, String file)根據(jù)指定 protocolhostport 號和 file 創(chuàng)建 URL 對象。
URLConnectionopenConnection()返回一個 URLConnection 對象,它表示到 URL 所引用的遠程對象的連接。
InputStream

openStream()

打開到此 URL 的連接并返回一個用于從該連接讀入的 InputStream
使用URL讀取內(nèi)容:
package org.demo.net;import java.io.InputStream;import java.net.MalformedURLException;import java.net.URL;import java.util.Scanner;public class UrlDemo {    public static void main(String[] args) {        URL url;        try {            //指定操作的URL            url = new URL("http","www.baidu.com",80,"/index.html");            //打開輸入流,讀取URL內(nèi)容            InputStream inputStream=url.openStream();            Scanner scan=new Scanner(inputStream);            //設(shè)置讀取分隔符            scan.useDelimiter("/n");            while(scan.hasNext()){                //輸出內(nèi)容                System.out.println(scan.next());            }        } catch (Exception e) {            e.printStackTrace();        }    }}

結(jié)果:

2222

顯示出來的是HTML代碼。

2.URLConnection

URLConnection是封裝訪問遠程網(wǎng)絡(luò)資源一般方法的類,通過它可以建立與遠程服務(wù)器的連接,檢查遠程資源的一些屬性。

常用方法:

類型方法描述
int

getContentLength()

返回 content-length 頭字段的值。
String

getContentType()

返回 content-type 頭字段的值。
InputStream

getInputStream()

返回從此打開的連接讀取的輸入流。

URLConnection對象可以通過URL類的openConnection()方法取得。

取得URL的基本信息:
package org.demo.net;import java.net.URL;import java.net.URLConnection;public class URLConnectionDemo {    public static void main(String[] args) {        try {            URL url=new URL("http://www.baidu.com");            //建立連接            URLConnection urlConn=url.openConnection();            System.out.println("內(nèi)容大小:"+urlConn.getContentLength());            System.out.println("內(nèi)容類型:"+urlConn.getContentType());        } catch (Exception e) {            e.printStackTrace();        }    }}
運行結(jié)果:
3333

三.URLEncoder與URLDecoder

在java中如果需要完成編碼和解碼操作就要使用URLEncoder和URLDecoder兩個類。

URLEncoder類的方法:

類型方法描述
static Stringencode(String s, String enc)使用指定的編碼機制將字符串轉(zhuǎn)換為 application/x-www-form-urlencoded 格式。

URLDecoder類的方法:

類型方法描述
static Stringdecode(String s, String enc)使用指定的編碼機制對 application/x-www-form-urlencoded 字符串解碼。
編碼及解碼操作:
package org.demo.net;import java.net.URLDecoder;import java.net.URLEncoder;public class CodeDemo {    public static void main(String[] args) {        String keyWord="oushine 陽";        try {            String enCode=URLEncoder.encode(keyWord, "UTF-8");            System.out.println("編碼之后:"+enCode);            String deCode=URLDecoder.decode(enCode, "UTF-8");            System.out.println("解碼之后:"+deCode);        } catch (Exception e) {            e.printStackTrace();        }            }}

運行結(jié)果:

5555


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 剑阁县| 盘锦市| 东乡族自治县| 金乡县| 隆化县| 荃湾区| 永德县| 湘潭市| 肥东县| 洛隆县| 松桃| 新密市| 浦江县| 荔浦县| 合山市| 曲松县| 开封市| 衡水市| 大洼县| 遂溪县| 大石桥市| 光山县| 葫芦岛市| 南宁市| 临安市| 沭阳县| 治县。| 阿图什市| 凌云县| 紫云| 东光县| 康平县| 军事| 保亭| 富宁县| 乌海市| 百色市| 中西区| 枝江市| 四会市| 四会市|