TNetHTTPRequest/TNetHTTPClientでWebサーバーにアクセスする

TNetHTTPRequestコンポーネントとTNetHTTPClientコンポーネントはXE8で追加された新しいコンポーネントです。

TNetHTTPRequestコンポーネントとTNetHTTPClientコンポーネントを使ってWebサーバーにアクセスするサンプルです。

nethttp01

TNetHTTPRequestコンポーネントとTNetHTTPClientコンポーネントをフォームに配置したら、
TNetHTTPRequestコンポーネントのClientプロパティをTNetHTTPClientコンポーネントに設定します。

Getメソッドでアクセスする

TNetHTTPRequestのGetメソッドを使用します。

var
  Url: string;
  ResponseContent: TMemoryStream;
  Encoding: TEncoding;
begin
  //アクセスするURL
  Url := 'http://www.gesource.jp/';
  //取得したデータを格納するStream
  ResponseContent := TMemoryStream.Create;
  //取得したデータのエンコーディング
  Encoding := TEncoding.GetEncoding(932);
  //URLにアクセスして、取得したデータをResponseContentに格納する
  NetHTTPRequest1.Get(Url, ResponseContent);
  //取得したデータをMemoコンポーネントに表示する
  Memo1.Lines.LoadFromStream(ResponseContent, Encoding);
end;

Postメソッドでアクセスする

TNetHTTPRequestのPostメソッドを使用します。

var
  URL: string;
  Source: TMultipartFormData;
  ResponseContent: TMemoryStream;
  Encoding: TEncoding;
begin
  // アクセスするURL
  URL := 'http://test2.localhost/index.php';
  // 送信するデータ
  Source := TMultipartFormData.Create;
  Source.AddField('param', 'value');
  // 取得したデータを格納するStream
  ResponseContent := TMemoryStream.Create;
  // 取得したデータのエンコーディング
  Encoding := TEncoding.UTF8;
  // URLにアクセスして、取得したデータをResponseContentに格納する
  NetHTTPRequest1.Post(URL, Source, ResponseContent);
  // 取得したデータをMemoコンポーネントに表示する
  Memo1.Lines.LoadFromStream(ResponseContent, Encoding);

HTTP応答を処理する

TNetHTTPRequestのOnRequestCompletedイベントで、応答データを取得できます。

procedure TForm1.Button3Click(Sender: TObject);
var
  URL: string;
begin
  // アクセスするURL
  URL := 'http://www.gesource.jp/';
  // OnRequestCompletedイベントを設定する
  NetHTTPRequest1.OnRequestCompleted := Self.RequestCompleted;
  // URLにアクセスする
  NetHTTPRequest1.Get(URL);
end;

procedure TForm1.RequestCompleted(const Sender: TObject;
  const AResponse: IHTTPResponse);
var
  Encoding: TEncoding;
begin
  // サーバーが使用する HTTP プロトコルのバージョン
  case AResponse.Version of
    THTTPProtocolVersion.UNKNOWN_HTTP:
      Memo1.Lines.Add('Version = UNKNOWN_HTTP');
    THTTPProtocolVersion.HTTP_1_0:
      Memo1.Lines.Add('Version = HTTP_1_0');
    THTTPProtocolVersion.HTTP_1_1:
      Memo1.Lines.Add('Version = HTTP_1_1');
    THTTPProtocolVersion.HTTP_2_0:
      Memo1.Lines.Add('Version = HTTP_2_0');
  end;
  // 応答のステータス コード
  Memo1.Lines.Add('StatusCode = ' + AResponse.StatusCode.ToString);
  // 応答の本体
  Encoding := TEncoding.GetEncoding(932); // エンコーディング
  Memo1.Lines.Add('Content = ' + AResponse.ContentAsString(Encoding));
end;

クッキー

TNetHTTPClientのAllowCookiesプロパティをTrueにすると、受け取ったクッキーをCookieManagerに格納します。

var
  URL: string;
  ResponseContent: TMemoryStream;
  Cookies: TCookiesArray;
  Cookie: TCookie;
begin
  //クッキーを受け入れる
  NetHTTPClient1.AllowCookies := True;

  // アクセスするURL
  URL := 'http://test2.localhost/index.php';
  // 取得したデータを格納するStream
  ResponseContent := TMemoryStream.Create;
  // URLにアクセスして、取得したデータをResponseContentに格納する
  NetHTTPRequest1.Get(URL, ResponseContent);

  // 取得したクッキーをMemoコンポーネントに表示する
  Cookies := NetHTTPClient1.CookieManager.Cookies;
  for Cookie in Cookies do
  begin
    Memo1.Lines.Add(Cookie.Name + ' = ' + Cookie.Value);
  end;
end;

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください