post url در دلفی از طریق http
function PostExample: string;
var
lHTTP: TIdHTTP;
lParamList: TStringList;
begin
lParamList := TStringList.Create;
lParamList.Add('id=1');
lHTTP := TIdHTTP.Create;
try
Result := lHTTP.Post('http://blahblahblah...', lParamList);
finally
lHTTP.Free;
lParamList.Free;
end;
end;
نمایش json به تفکیک
uses system.json
procedure TForm1.Button2Click(Sender: TObject);
var
JSonObject:TJSonObject;
st :='{"code":200,"message":"OK","data":{"code":3271,"mobile":"1"}}';
JSonValue:TJSonValue;
st:string;
Begin
JsonValue:=JSonObject.ParseJSONValue(st);
JSonObject := TJSonObject.Create;
memo1.text:=(JsonValue as TJSONObject).Get('code').JsonValue.Value;
// JsonValue:=(JsonValue as TJSONObject).Get('data').JsonValue;
JSonObject.Free;
end;
==================
نمونه دیگر ارسال
procedure TForm2.Button1Click(Sender: TObject);
var
IdHTTP: TIdHTTP;
ResponseContent: string;
JSONContent: TStringStream;
IOHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
try
IdHTTP := TIdHTTP.Create(nil);
IOHandler:= TIdSSLIOHandlerSocketOpenSSL.Create(NIL);
IOHandler.SSLOptions.SSLVersions := [sslvTLSv1, sslvSSLv2, sslvSSLv3];
IdHTTP.IOHandler := IOHandler;
IdHTTP.Request.ContentType := 'application/json; charset=utf-8';
IdHTTP.Request.AcceptEncoding := 'gzip, deflate, br';
IdHTTP.Request.AcceptLanguage := 'Accept-Language';
IdHTTP.Request.CustomHeaders.AddValue('apiKey', 'XXXXXXXXXXXXXXXXXX');
IdHTTP.Request.CustomHeaders.AddValue('accept-language', 'fa');
JSONContent := TStringStream.Create('{"mobile": "XXXX","method": "sms"}', TEncoding.UTF8);
ResponseContent := IdHTTP.Post('https://url', JSONContent);
showmessage(ResponseContent); // Display response content
finally
JSONContent.Free;
FreeAndNil(IdHttp);
end;
end;