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

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