URL的使用

1.URL的初始化

  1. public URL(String spec);
URL urlbase=new URL("http://my.oschina.net/u/2308739/admin/new-blog.html");
  1. public URL(URL context,String spec);
URL urlbase=new URL("http://my.oschina.net/u/2308739/admin/");
URL indexUrl=new URL(urlbase,"new-blog.html");
  1. public URL(String protocol,String host,String file);
    通过协议名、主机名、文件名构造一个URL对象。
new URL("http","http://my.oschina.net","/u/2308739/admin/new-blog.html";
  1. public URL(String protocol,String host,int port,String file);
    通过协议名、主机名、端口号,文件名构造一个URL对象。
new URL("http","http://my.oschina.net",80,"/u/2308739/admin/new-blog.html");

2.实例通过URL读内容

测试代码:

public static void main(String[] args) {
    try {
        URL ifeng=new URL("http://www.ifeng.com/");
        BufferedReader bReader=new
            BufferedReader(new InputStreamReader(ifeng.openStream()));
        String inputLineString;
        while((inputLineString=bReader.readLine())!=null){
            System.out.println(inputLineString);
        }
        bReader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

输出结果:

<!DOCTYPE html>
<html xmlns:wb="http://open.weibo.com/wb">
...
</html>

3.实例通过URLConnection读内容

测试代码:

public static void main(String[] args) {
    try {
        URL ifeng = new URL("http://www.ifeng.com/");
        URLConnection urlConnection = ifeng.openConnection();
        BufferedReader bReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        String inputLineString;
        while ((inputLineString = bReader.readLine()) != null) {
            System.out.println(inputLineString);
        }
        bReader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

输出结果:

<!DOCTYPE html>
<html xmlns:wb="http://open.weibo.com/wb">
...
</html>

4.实例通过URLConnection写内容

测试代码:

public static void main(String[] args) {
    try {
        // Configure and open a connection to the site you will send the request
        URL url = new URL("http://wx.tclha.com/login.do");
        URLConnection urlConnection = url.openConnection();

        // 设置doOutput属性为true表示将使用此urlConnection写入数据
        urlConnection.setDoOutput(true);
        // 定义待写入数据的内容类型,我们设置为application/x-www-form-urlencoded类型
        urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
        HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;//httpurlconection 有可以获得返回状态值的方法。
        /** httpurlconnetion一些配置属性 **/
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // httpURLConnection.setRequestProperty("Content-Length", String.valueOf(parameterData.length()));
        // 得到请求的输出流对象
        OutputStreamWriter out = new OutputStreamWriter(httpURLConnection.getOutputStream());
        // 把数据写入请求的Body
        out.write("userName=yuanw&passWord=121212");
        out.flush();
        out.close();
        System.out.println("ResponseCode:" + httpURLConnection.getResponseCode());
        if (httpURLConnection.getResponseCode() >= 300) {
            System.out.println("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
        }
        // 从服务器读取响应
        InputStream inputStream = urlConnection.getInputStream();
        String encoding = urlConnection.getContentEncoding();
        System.out.println("encoding:" + encoding);
        String body = IOUtils.toString(inputStream, "utf-8");
        System.out.println(body);
    } catch (IOException e) {
        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, e);
    }
}

输出结果:

ResponseCode:500
HTTP Request is not success, Response code is 500
...

由于网站不再更新,请求会报错。。。

补充:
关于URL和URI的详细介绍:https://www.cnblogs.com/throwable/p/9740425.html





参考文章:https://my.oschina.net/u/2308739/blog/410562

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章