HttpURLConnection
| 日本語 | 超文書定型資源位置表示接続 |
| 英語 | Hypertext transfer protocol Uniform Resource Locator connection |
| ふりがな | えっちてぃーてぃーぴーゆーあーるえるこねくしょん、えいちてぃーてぃーぴーゆーあーるえるこねくしょん |
| フリガナ | エッチティーティーピーユーアールエルコネクション、エイチティーティーピーユーアールエルコネクション |
J2SEに含まれるクラスのひとつ。パッケージも含めたクラス名はjava.net.HttpURLConnection
。
ホームページにアクセスするためのクラス。
java.net.URLクラスでアクセスするURLを指定し、そのopenConnection()メソッドを呼び出すことで得られるURLConnectionクラスをHttpURLConnectionクラスにダウンキャストすることで取得する。
setRequestMethod()メソッドでHTTPメソッドを指定し、getInputStream()メソッドを呼び出すことでストリームクラスを取得できるため、これを用いてホームページを取得する。
。
ホームページにアクセスするためのクラス。
java.net.URLクラスでアクセスするURLを指定し、そのopenConnection()メソッドを呼び出すことで得られるURLConnectionクラスをHttpURLConnectionクラスにダウンキャストすることで取得する。
setRequestMethod()メソッドでHTTPメソッドを指定し、getInputStream()メソッドを呼び出すことでストリームクラスを取得できるため、これを用いてホームページを取得する。
// 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();
}
}
}
}




