java net 学习(二)--URL

URL

一个URL,一般来说由2部分组成:

protocol   eg  http , ftp

resourcename  eg  www.google.com.hk

 

resourcename由4部分组成

hostname   eg www.google.com.hk

filename   eg /path/file , 是文件在server上的路径

port   eg 连接的端口

reference  用来标识file的

 

而一般来说hostname 和filename会被 使用,而port 和reference不太使用

 

创建:

1.    直接:  URL googleHK = new URL(http://www.google.com.hk );

2.    使用BaseURL   URL googleHKmap = new URL(googleHK, “/map”);

如果前面的baseURL为空,则默认后面的为完全的URL

3.    分开各个部分:

new URL(“http”,”www.google.com.hk”,”/map.html”);

4.如果有特殊字符,则需转义,如空格

    www.google.com/hello world /

定义时: new URL(“www.google.com/hello%20world / ”)

也可以定义成 URI ,这时java会为你转化

new URI (“http”,”www.google.com”,”/hello world/”);

URL解析方法:

getProtocol

Returns the protocol identifier component of the URL.

getAuthority

Returns the authority component of the URL.

getHost

Returns the host name component of the URL.

getPort

Returns 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.

getPath

Returns the path component of this URL.

getQuery

Returns the query component of this URL.

getFile

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

getRef

Returns the reference component of the URL.

 

 

直接从URL读取内容

URL yahoo = new URL(“http://www.yahoo.com ”);

BufferedReader reader = new BufferedReader(new InputStreamReader(yahoo.openStream()));

String inputLine;

While((inputLine = reader.readLine()) != null){

System.out.println(inputLine);

}

reader.close();

 

 

打开URLConnection,进行read , write

import java.net.*;

import java.io.*;

 

public class URLConnectionReader



 {

    

public static void main(String[] args) throws Exception {

        

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

        

URLConnection yc = yahoo.openConnection();





        

BufferedReader in = new BufferedReader(

                                

new InputStreamReader(

                                

yc.getInputStream()



));

        

String inputLine;

 

        

while ((inputLine = in.readLine()) != null) 

            

System.out.println(inputLine);

        

in.close();

    

}

}

 

public class Reverse {

    public static void main(String[] args) throws Exception {

 

        if (args.length != 2) {

            System.err.println("Usage:  java Reverse " +

                               "http://<location of your servlet/script>" +

                                " string_to_reverse");

            System.exit(1);

        }

 

        String stringToReverse = URLEncoder.encode(args[1], "UTF-8");

 

        URL url = new URL(args[0]);

        URLConnection connection = url.openConnection();

        connection.setDoOutput(true);

 

        OutputStreamWriter out = new OutputStreamWriter(

                              connection.getOutputStream());

        out.write("string=" + stringToReverse);

        out.close();

 

        BufferedReader in = new BufferedReader(

                               new InputStreamReader(

                               connection.getInputStream()));

                              

        String decodedString;

 

        while ((decodedString = in.readLine()) != null) {

            System.out.println(decodedString);

        }

        in.close();

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值