4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

簡単にHTTPS通信ができるTHTTPClient

Posted at

はじめに

DelphiでHTTP通信をするときにIndyを使っていた人は多いと思います
ですがDelphiXE8あたりからもっと便利なTHTTPClientクラスが用意されていますので是非使ってみましょう

なにが違うの?

外部DLLなしでHTTPS通信に対応
http2に対応
難しい設定が不要

使う前の準備

usesに System.Net.HttpClientを追加します

delphi.pas
use System.Net.HttpCliet

getメソッドのサンプル

urlを指定してgetメソッドを実行して返信を返すソースはたったこれだけ

delphi.pas
function HttpGet(const Url: string): string;
var
  http: THTTPClient;
  res: IHTTPResponse;
  stream: TSTringStream;
  ts : TStringList;
begin
  result := '';
  http := THTTPClient.Create;
  stream := TStringStream.Create;
  ts := TStringList.Create;
  try
    res := http.Get(Url, stream);
    ts.LoadFromStream((res).ContentStream, TEncoding.UTF8);
    result := ts.Text;
  finally
    ts.Free;
    stream.Free;
    http.Free;
  end;
end;

getメソッドで画像などのバイナリデータを取得

返り値はストリーム型なので必要に応じたクラスで読み込めば完成します

delphi.pas
function HttpPicture.Get(const Url, aFilename: string): Boolean;
var
  http: THTTPClient;
  res: IHTTPResponse;
  stream: TSTringStream;
  ts : TStringList;
  png : TPngImage;
  jpg : TJPEGImage;
  bmp : TBitmap;
  s : string;
begin
  result := False;
  http := THTTPClient.Create;
  ts := TStringList.Create;
  png := TPngImage.Create;
  jpg := TJPEGImage.Create;
  bmp := TBitmap.Create;
  try
    res := http.Get(Url); 
    if res.StatusCode = 200 then begin
      s := res.MimeType;
      try
      if Pos('jp',s) <> 0 then begin
        jpg.LoadFromStream( res.ContentStream);
        jpg.SaveToFile(aFilename);
      end;
      if Pos('png',s) <> 0 then begin
        png.LoadFromStream( res.ContentStream);
        png.SaveToFile(aFilename);
      end;
      if Pos('bmp',s) <> 0 then begin
        bmp.LoadFromStream( res.ContentStream);
        bmp.SaveToFile(aFilename);
      end;
      result := True;
      except
      result := False;
      end;
    end;
  finally
    bmp.Free;
    jpg.Free;
    png.Free;
    ts.Free;
    http.Free;
  end;
end;

postメソッドの罠

簡素化を目指したが故にpostの仕様はやっかいですが
下記の3種類だけ覚えましょう、覚えれば簡単です

delphi.pas
var
  http: THTTPClient;
  res: IHTTPResponse;
  str : string
  stream: TSTringStream;
  ts : TStringList;
begin
  res := http.Post(Url,ts);     // フォームの内容を送る形式
  res := http.Post(url,stream); // JSONや画像ファイルを送る形式
  res := http.Post(url,str);    // strで指定したファイルを送る形式?
end;

postメソッドのサンプル

JSONをpostで送信するサンプルです

delphi.pas
function THttp2.Post(const Url: string;Param: string): string;
var
  http: THTTPClient;
  res: IHTTPResponse;
  stream: TSTringStream;
  ts : TStringList;
begin
  result := '';
  http := THTTPClient.Create;
  stream := TStringStream.Create;
  ts := TStringList.Create;
  try
    http.ContentType := 'application/json;charset=utf-8';
    stream.WriteString(Param);
    stream.Position := 0;
    res := http.Post(Url,stream);
    ts.LoadFromStream((res).ContentStream, TEncoding.UTF8);
    result := ts.Text;
  finally
    ts.Free;
    stream.Free;
    http.Free;
  end;
end;

最後に

いかがでしょうか?
やっかいなhttps通信も簡単に行う事が出来ますので是非使ってみましょう

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?