URI
| 日本語 | 定型資源識別子 |
| 英語 | Uniform Resource Identifier |
| ふりがな | ゆーあーるあい |
| フリガナ | ゆーあーるえる |
URLのようなもの。
URLはURIの一種であり、URLの構文はURIによって定義されている。そのため、URIとURLは似ている。
ただし、URLは「インターネット上の位置を特定するもの」であるのに対し、URIは必ずしも特定しない。つまり、URLは「絶対パス」のように機能するが、URIの場合は「絶対パス」に加えて「ファイル名」や「相対パス」のようなものも含んでいる。
だが、ほとんどの場合はURLで十分であり、URIを使用することはほとんどないだろう。
URIの管理にはjava.net.URIクラスを使用する。
URLはURIの一種であり、URLの構文はURIによって定義されている。そのため、URIとURLは似ている。
ただし、URLは「インターネット上の位置を特定するもの」であるのに対し、URIは必ずしも特定しない。つまり、URLは「絶対パス」のように機能するが、URIの場合は「絶対パス」に加えて「ファイル名」や「相対パス」のようなものも含んでいる。
だが、ほとんどの場合はURLで十分であり、URIを使用することはほとんどないだろう。
URIの管理にはjava.net.URIクラスを使用する。
// Sample.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
public class Sample
{
public static void main( String[] args )
{
BufferedReader bufferedReader = null;
HttpURLConnection httpURLConnection = null;
try
{
// プロトコル。
String protocol = "http";
// ホストを示すドメイン名。
String host = "yahoo.co.jp";
// ポート番号。
int port = 80;
// ファイルパス。
String filePath = "/index.html";
// アクセスするためのURIクラスを作ります。
URI uri = new URI( protocol, "", host, port, filePath, "", "" );
// それを元にURLを取得します。
URL url = uri.toURL();
System.out.println( url.toString() );
// http://www.yahoo.co.jp:80/index.html
// Content-Typeの文字コード(注:実際には、HEADで文字コードを取得してから指定します)。
String charSet = "EUC-JP";
// リクエストのメソッド。
String method = "GET";
// 指定されたURLを元にリクエストを発行します。
httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod( method );
// リクエストの結果を取得します。
InputStreamReader inputStreamReader = new InputStreamReader( httpURLConnection.getInputStream(), charSet );
bufferedReader = new BufferedReader( inputStreamReader );
// 全行出力します。
while( true )
{
String oneLine = bufferedReader.readLine();
if( oneLine == null )
{
break;
}
System.out.println( oneLine );
}
// <html>
// <head>
// <title>Yahoo! JAPAN</title>
// (以下略)
}
catch( URISyntaxException e )
{
// URIに誤りがあるとURISyntaxException例外が投げられます。
e.printStackTrace();
}
catch( IOException e )
{
e.printStackTrace();
}
finally
{
if( bufferedReader != null )
{
try
{
bufferedReader.close();
}
catch( IOException e )
{
e.printStackTrace();
}
}
if( httpURLConnection != null )
{
httpURLConnection.disconnect();
}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
public class Sample
{
public static void main( String[] args )
{
BufferedReader bufferedReader = null;
HttpURLConnection httpURLConnection = null;
try
{
// プロトコル。
String protocol = "http";
// ホストを示すドメイン名。
String host = "yahoo.co.jp";
// ポート番号。
int port = 80;
// ファイルパス。
String filePath = "/index.html";
// アクセスするためのURIクラスを作ります。
URI uri = new URI( protocol, "", host, port, filePath, "", "" );
// それを元にURLを取得します。
URL url = uri.toURL();
System.out.println( url.toString() );
// http://www.yahoo.co.jp:80/index.html
// Content-Typeの文字コード(注:実際には、HEADで文字コードを取得してから指定します)。
String charSet = "EUC-JP";
// リクエストのメソッド。
String method = "GET";
// 指定されたURLを元にリクエストを発行します。
httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod( method );
// リクエストの結果を取得します。
InputStreamReader inputStreamReader = new InputStreamReader( httpURLConnection.getInputStream(), charSet );
bufferedReader = new BufferedReader( inputStreamReader );
// 全行出力します。
while( true )
{
String oneLine = bufferedReader.readLine();
if( oneLine == null )
{
break;
}
System.out.println( oneLine );
}
// <html>
// <head>
// <title>Yahoo! JAPAN</title>
// (以下略)
}
catch( URISyntaxException e )
{
// URIに誤りがあるとURISyntaxException例外が投げられます。
e.printStackTrace();
}
catch( IOException e )
{
e.printStackTrace();
}
finally
{
if( bufferedReader != null )
{
try
{
bufferedReader.close();
}
catch( IOException e )
{
e.printStackTrace();
}
}
if( httpURLConnection != null )
{
httpURLConnection.disconnect();
}
}
}
}
// Sample.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
public class Sample
{
public static void main( String[] args )
{
BufferedReader bufferedReader = null;
HttpURLConnection httpURLConnection = null;
try
{
// プロトコル。
String protocol = "http";
// ホストを示すドメイン名。
String host = "yahoo.co.jp";
// ポート番号。
int port = 80;
// ファイルパス。
String filePath = "/index.html";
// アクセスするためのURIクラスを作ります。
URI uri = new URI( protocol, "", host, port, filePath, "", "" );
// それを元にURLを取得します。
URL url = uri.toURL();
System.out.println( url.toString() );
// http://www.yahoo.co.jp:80/index.html
// Content-Typeの文字コード(注:実際には、HEADで文字コードを取得してから指定します)。
String charSet = "EUC-JP";
// リクエストのメソッド。
String method = "GET";
// 指定されたURLを元にリクエストを発行します。
httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod( method );
// リクエストの結果を取得します。
InputStreamReader inputStreamReader = new InputStreamReader( httpURLConnection.getInputStream(), charSet );
bufferedReader = new BufferedReader( inputStreamReader );
// 全行出力します。
while( true )
{
String oneLine = bufferedReader.readLine();
if( oneLine == null )
{
break;
}
System.out.println( oneLine );
}
// <html>
// <head>
// <title>Yahoo! JAPAN</title>
// (以下略)
}
catch( URISyntaxException e )
{
// URIに誤りがあるとURISyntaxException例外が投げられます。
e.printStackTrace();
}
catch( IOException e )
{
e.printStackTrace();
}
finally
{
if( bufferedReader != null )
{
try
{
bufferedReader.close();
}
catch( IOException e )
{
e.printStackTrace();
}
}
if( httpURLConnection != null )
{
httpURLConnection.disconnect();
}
}
}
}




