URL類中包含了很多方法用於訪問URL的各個部分,具體方法及描述如下:
序號 | 方法描述 |
---|---|
1 |
public String getPath() 返回URL路徑部分。 |
2 |
public String getQuery() 返回URL查詢部分。 |
3 |
public String getAuthority() 獲取此 URL 的授權部分。 |
4 |
public int getPort() 返回URL端口部分 |
5 |
public int getDefaultPort() 返回協議的默認端口號。 |
6 |
public String getProtocol() 返回URL的協議 |
7 |
public String getHost() 返回URL的主機 |
8 |
public String getFile() 返回URL文件名部分 |
9 |
public String getRef() 獲取此 URL 的錨點(也稱爲"引用")。 |
10 |
public URLConnection openConnection() throws IOException 打開一個URL連接,並運行客戶端訪問資源。 |
openConnection() 返回一個 java.net.URLConnection。
我連接的是HTTP協議的URL,所以openConnection() 方法返回 HttpURLConnection 對象。
URLConnection 方法列表如下:
序號 | 方法描述 |
---|---|
1 |
Object getContent() 檢索URL鏈接內容 |
2 |
Object getContent(Class[] classes) 檢索URL鏈接內容 |
3 |
String getContentEncoding() 返回頭部 content-encoding 字段值。 |
4 |
int getContentLength() 返回頭部 content-length字段值 |
5 |
String getContentType() 返回頭部 content-type 字段值 |
6 |
int getLastModified() 返回頭部 last-modified 字段值。 |
7 |
long getExpiration() 返回頭部 expires 字段值。 |
8 |
long getIfModifiedSince() 返回對象的 ifModifiedSince 字段值。 |
9 |
public void setDoInput(boolean input) URL 連接可用於輸入和/或輸出。如果打算使用 URL 連接進行輸入,則將 DoInput 標誌設置爲 true;如果不打算使用,則設置爲 false。默認值爲 true。 |
10 |
public void setDoOutput(boolean output) URL 連接可用於輸入和/或輸出。如果打算使用 URL 連接進行輸出,則將 DoOutput 標誌設置爲 true;如果不打算使用,則設置爲 false。默認值爲 false。 |
11 |
public InputStream getInputStream() throws IOException 返回URL的輸入流,用於讀取資源 |
12 |
public OutputStream getOutputStream() throws IOException 返回URL的輸出流, 用於寫入資源。 |
13 |
public URL getURL() 返回 URLConnection 對象連接的URL |
public class URLConnectionTest
{
public static void main(String[] args)
{
try
{
URL url = new URL("http://localhost:8080/http/test.html");
//獲得URLConnection對像
URLConnection conn = url.openConnection();
//判斷是否是HTTP協議的URL,如果是,獲得網絡連接對像
HttpURLConnection httpConn;
if(conn instanceof HttpURLConnection){
httpConn = (HttpURLConnection)conn;
//獲得URLConnection的InputStream
BufferedReader br = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
/*獲得URLConntion連接的頭信息*/
Map<String,List<String>> headerMap = httpConn.getHeaderFields();
for(String key: headerMap.keySet()){
//輸出頭字段
System.out.println(key+"--->"+headerMap.get(key));
}
System.out.println("HeaderFields"+httpConn.getHeaderFields());
System.out.println("Content-Length:"+httpConn.getContentLength());
System.out.println("Content-Type:"+httpConn.getContentType());
System.out.println("Content-Encoding:"+httpConn.getContentEncoding());
System.out.println("----------------------------------------");
//輸出InputStream
String line=null;
while((line=br.readLine()) != null){
//輸出內容爲原始的HTML標記內容
System.out.println(line);
}
}
}catch (MalformedURLException e)
{
e.printStackTrace();
}catch (IOException e)
{
e.printStackTrace();
}
}
}
InputStream
URL url = new URL("http://localhost:8080/http/test.html");
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String line=null;
while((line=br.readLine()) != null){
//輸出內容爲原始的HTML標記內容
System.out.println(line);
}
URL url = new URL("http://localhost:8080/http/test.html");
//獲得URLConnection對像
URLConnection conn = url.openConnection();
//獲得URLConnection的InputStream
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
/*獲得URLConntion連接的頭信息*/
Map<String,List<String>> headerMap = conn.getHeaderFields();
for(String key: headerMap.keySet()){
//輸出頭字段
System.out.println(key+":"+headerMap.get(key));
}
System.out.println();
System.out.println("Content-Length:"+conn.getContentLength());
System.out.println("Content-Type:"+conn.getContentType());
System.out.println("Content-Encoding:"+conn.getContentEncoding());
System.out.println("Date:"+conn.getDate());
System.out.println("Last-Modified:"+conn.getLastModified());
//輸出InputStream
String line=null;
while((line=br.readLine()) != null){
//輸出內容爲原始的HTML標記內容
System.out.println(line);
}