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;
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);
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;
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;
var NetHTTPClient : TNetHTTPClient;
parametres : Tstringlist;
begin
parametres := tstringlist.Create;
NetHTTPClient := TNetHTTPClient.Create(nil);
parametres .Add('nom_logiciel='+UTF8Encode('éè'));
parametres .Add('emplacement='+EncodeURIComponent('éè') );
parametres .Add('version='+ 'éè');
NetHTTPClient.ContentType := 'application/json';
NetHTTPClient.AcceptEncoding := 'UTF-8';
NetHTTPClient.post('http://metaclients.xxx.fr/xxx',parametres,nil,
TEncoding.UTF8).ContentAsString(tencoding.UTF8);
parametres .Free;
NetHTTPClient.Free;
end;
var
NetHTTPClient : TNetHTTPClient;
parametres : TStringStream;
begin
NetHTTPClient := TNetHTTPClient.Create(nil);
try
NetHTTPClient.ContentType := 'application/json';
NetHTTPClient.AcceptEncoding := 'UTF-8';
parametres := TStringStream.Create('[{"sku": "AS12AS11S", "estoque": 3}, {"sku": "ASKD123F4", "estoque": 10}]', TEncoding.UTF8);
try
Result := NetHTTPClient.Post('http://metaclients.xxx.fr/xxx', parametres, nil, TEncoding.UTF8).ContentAsString(TEncoding.UTF8);
finally
parametres.Free;
end;
finally
NetHTTPClient.Free;
end;
end;
IdHTTP1.CookieManager.CookieCollection[i].Comment
IdHTTP1.CookieManager.CookieCollection[i].MaxAge
IdHTTP1.CookieManager.CookieCollection[i].Version
IdHTTP1.CookieManager.CookieCollection[i].CookieText
IdHTTP1.CookieManager.CookieCollection[i].Domain
IdHTTP1.CookieManager.CookieCollection[i].Expires
IdHTTP1.CookieManager.CookieCollection[i].CookieName
IdHTTP1.CookieManager.CookieCollection[i].Path
IdHTTP1.CookieManager.CookieCollection[i].Secure
IdHTTP1.CookieManager.CookieCollection[i].Value
unit Unit1;
interface
uses
IdHTTP,
Dialogs,
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses IdCookieManager, IdCookie;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
IdHTTP: TIdHTTP;
stringstream: TStringStream;
i: Integer;
begin
IdHTTP:=TIdHTTP.Create(nil);
IdHTTP.AllowCookies:=True;
<i>{
TIdCookieManager создавать не будем, ибо (Delphi 7):
unit IdHTTP
procedure TIdCustomHTTP.ProcessCookies(ARequest: TIdHTTPRequest; AResponse: TIdHTTPResponse);
...
begin
...
if not Assigned(FCookieManager) and AllowCookies then
begin
CookieManager := TIdCookieManager.Create(Self);
FFreeOnDestroy := true;
end;
...
}
</i> IdHTTP.HandleRedirects:=True;
// POST request
stringstream:=TStringStream.Create('vb_login_username=имя&vb_login_password=пароль&cookieuser=1&s=&securitytoken=guest&do=login&vb_login_md5password=&vb_login_md5password_utf=');
try
IdHTTP.Request.ContentType:='application/x-www-form-urlencoded';
IdHTTP.Request.ContentLength:=stringstream.Size;
IdHTTP.Post('http://forum29.ru/login.php?do=login', stringstream);
// не дождетесь raise
if IdHTTP.CookieManager=nil then raise Exception.Create('CookieManager=nil');
// в Edit1 соберем cookie
Edit1.Text:='';
for i:=0 to IdHTTP.CookieManager.CookieCollection.Count-1 do
Edit1.Text:=Edit1.Text+IdHTTP.CookieManager.CookieCollection[i].CookieName+'='+IdHTTP.CookieManager.CookieCollection[i].Value+';';
finally
stringstream.Free;
IdHTTP.Free;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
IdHTTP: TIdHTTP;
begin
IdHTTP:=TIdHTTP.Create(nil);
// руками добавим cookie
IdHTTP.AllowCookies:=False;
IdHTTP.HandleRedirects:=True;
try
// в Edit1 наши cookie
IdHTTP.Request.CustomHeaders.Add('Cookie: '+Edit1.Text);
Memo1.Text:=IdHTTP.Get('http://forum29.ru/');
Memo1.Lines.SaveToFile('c:\Downloads\forum29.htm');
finally
IdHTTP.Free;
end;
end;
end.
uses System.Net.URLClient,System.Net.HttpClient, System.Net.HttpClientComponent;
function HttpPost(Url: String; ID: Integer; Cookie: string): String;
var
NetHttp: TNetHTTPClient;
TmpSS: TStringStream;
ParmList: TStringList;
begin
Result := '';
NetHttp := TNetHTTPClient.Create(nil);
ParmList := TStringList.Create;
TmpSS := TStringStream.Create('', TEncoding.UTF8);
try
//NetHttp.Asynchronous := True; 异步
NetHttp.ConnectionTimeout := 10000;
NetHttp.ResponseTimeout := 20000;
// NetHttp.CustomHeaders['Cookie'] := 'sscSESSID=' + Cookie;
NetHttp.UserAgent := 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36';
ParmList.Add('op=getCurIssue');
ParmList.Add('op=getCurIssue');
ParmList.Add('lotteryId=' + IntToStr(ID));
NetHttp.Post(Url, ParmList, TmpSS);
Result := Trim(TmpSS.DataString);
finally
NetHttp.Free;
ParmList.Free;
TmpSS.Free;
end;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
Memo1.Text:= HttpPost('http://www.baidu.com/',1,'123');
end;