Credential: INetHttpCommandCredential;
The Credential property returns credentials that are used to identify the user in network resource.
Executing the example requires a deployed REST-based web service. The service must contain GET request handler with the parameters specified in the example. The service also supposes authentication before sending requests.
Add links to the Collections, Dt, IO, Net system assemblies.
Sub UserProc;
Var
Service: INetHttpService;
Cmd: INetCommand;
HttpCmd: INetHttpCommand;
Rest: IDtRestProvider;
Creds: INetHttpCommandCredential;
Request, Response: Variant;
MemStream: IIOStream;
TReader: ITextReader;
Begin
Service := New NetHttpService.Create;
Service.Url := "http://www.sample.com/api/request?key=12345&id=Sample";
Cmd := Service.Connect;
HttpCmd := Cmd As INetHttpCommand;
Rest := New DtRestProvider.Create;
Rest.Timeout := 10000;
//Credentials for authentication on connection
Creds := HttpCmd.Credential;
Creds.Method := NetHttpCommandAuthentication.Basic;
Creds.User := "user";
Creds.Password := "password";
//Additional parameters of requests execution
HttpCmd.Command := NetHttpCommandType.Get_;
HttpCmd.AcceptLanguage := "ru";
HttpCmd.OuterBody := True;
HttpCmd.ConnectionTimeout := 10000;
HttpCmd.Timeout := 10000;
HttpCmd.Headers.Add("Accept: application/json");
Request := "";
//GET request execution
Cmd.Start(Request);
Response := Cmd.Result;
If (Response.IsNull) Then
Debug.WriteLine("Empty response is obtained");
Else
If Response Is String Then
Debug.WriteLine(Response As String);
Else
MemStream := New MemoryStream.Create;
MemStream.CopyFrom(IOStream.FromVariant(Response), -1);
MemStream.Position := 0;
TReader := New TextReader.Create(MemStream);
TReader.Encoding := CodePage.UTF8;
TReader.ReadToEnd;
//Further work with obtained data stream
End If;
End If;
End Sub UserProc;
On executing the example, the connection to the specified web service is established with authentication of the specified user. After connection, the GET request to web service is executed. There are ten seconds to execute the request and to get the response. If the obtained result is a string, the value will be displayed to the development environment console, otherwise the stream to work with obtained data will be created.
See also: