URL
| 日本語 | 定型資源位置表示 |
| 英語 | Uniform Resource Locator |
| ふりがな | ゆーあーるえる |
| フリガナ | ユーアールエル |
ネット上の情報の位置を指し示す文字列。
「http://www.yahoo.co.jp/」といった文字列のこと。
この文字列で、ネット上のページにアクセスすることができる。
URLは以下のような構造となる。
プロトコル://ホスト:ポート番号/ファイルパス?リクエストパラメーター
たとえば、下記文字列は「プロトコル:HTTP」「ホスト:www.kab-studio.biz」「ポート番号:80」「ファイルパス:Programing/JavaA2Z/search」「リクエストパラメーター:query=URL」を指定したURLである。
http://www.kab-studio.biz:80/Programing/JavaA2Z/search?query=URL
URLには半角英数字しか使用できないため、ファイルパスやリクエストパラメーターに記号や日本語の文字列を使用する場合にはURLEncoderクラスを使用して変換する必要がある。
URLの管理にはjava.net.URLクラスを使用する。
このクラスのopenConnection()メソッドで取得できるHttpURLConnectionクラスを使用することで、簡単にホームページを取得することができる。
「http://www.yahoo.co.jp/」といった文字列のこと。
この文字列で、ネット上のページにアクセスすることができる。
URLは以下のような構造となる。
プロトコル://ホスト:ポート番号/ファイルパス?リクエストパラメーター
たとえば、下記文字列は「プロトコル:HTTP」「ホスト:www.kab-studio.biz」「ポート番号:80」「ファイルパス:Programing/JavaA2Z/search」「リクエストパラメーター:query=URL」を指定したURLである。
http://www.kab-studio.biz:80/Programing/JavaA2Z/search?query=URL
URLには半角英数字しか使用できないため、ファイルパスやリクエストパラメーターに記号や日本語の文字列を使用する場合にはURLEncoderクラスを使用して変換する必要がある。
URLの管理にはjava.net.URLクラスを使用する。
このクラスのopenConnection()メソッドで取得できるHttpURLConnectionクラスを使用することで、簡単にホームページを取得することができる。
// Sample.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.HttpURLConnection;
public class Sample
{
public static void main( String[] args )
{
BufferedReader bufferedReader = null;
HttpURLConnection httpURLConnection = null;
try
{
// プロトコル。
String protocol = "http";
// ホストを示すドメイン名。
String host = "www.yahoo.co.jp";
// ポート番号。
int port = 80;
// ファイルパス。
String filePath = "/index.html";
// アクセスするためのURLクラスを作ります。
URL url = new URL( protocol, host, port, filePath );
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( 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.URL;
import java.net.HttpURLConnection;
public class Sample
{
public static void main( String[] args )
{
BufferedReader bufferedReader = null;
HttpURLConnection httpURLConnection = null;
try
{
// プロトコル。
String protocol = "http";
// ホストを示すドメイン名。
String host = "www.yahoo.co.jp";
// ポート番号。
int port = 80;
// ファイルパス。
String filePath = "/index.html";
// アクセスするためのURLクラスを作ります。
URL url = new URL( protocol, host, port, filePath );
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( 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.URL;
import java.net.HttpURLConnection;
public class Sample
{
public static void main( String[] args )
{
BufferedReader bufferedReader = null;
HttpURLConnection httpURLConnection = null;
try
{
// プロトコル。
String protocol = "http";
// ホストを示すドメイン名。
String host = "www.yahoo.co.jp";
// ポート番号。
int port = 80;
// ファイルパス。
String filePath = "/index.html";
// アクセスするためのURLクラスを作ります。
URL url = new URL( protocol, host, port, filePath );
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( IOException e )
{
e.printStackTrace();
}
finally
{
if( bufferedReader != null )
{
try
{
bufferedReader.close();
}
catch( IOException e )
{
e.printStackTrace();
}
}
if( httpURLConnection != null )
{
httpURLConnection.disconnect();
}
}
}
}




