14. Dezember 2021 18:00
LOCAL [TryFunction] CheckResponse(HttpRequest : DotNet "System.Net.HttpWebRequest")
HttpResponse := HttpRequest.GetResponse;
HttpStatus := HttpResponse.StatusCode;
6. Januar 2022 17:18
Uri := Uri.Uri(Url);
BaseUrl := Uri.GetLeftPart(UriPartial.Authority);
QueryUrl := Uri.AbsolutePath + Uri.Query;
Method := 'GET';
User := 'TestUser';
Pass := 'TestPass';
HttpClient := HttpClient.HttpClient();
HttpClient.DefaultRequestHeaders.Add('ApiKey','Testkey');
HttpClient.DefaultRequestHeaders.Add('Secret','TestSecret');
HttpClient.BaseAddress := Uri.Uri(BaseUrl);
HttpClient.Timeout := 5000;
//Auth
IF User+Pass <> '' THEN BEGIN
byteArray := Encoding.ASCII.GetBytes(User+':'+Pass);
HttpClient.DefaultRequestHeaders.Authorization := AuthenticationHeaderValue.AuthenticationHeaderValue('Basic', Convert.ToBase64String(byteArray));
END;
CASE Method OF
'GET':
HttpResponseMessage := HttpClient.GetAsync(QueryUrl).Result;
'POST':
HttpResponseMessage := HttpClient.PostAsync(QueryUrl,HttpContent).Result;
'PUT':
HttpResponseMessage := HttpClient.PutAsync(QueryUrl,HttpContent).Result;
'DELETE':
HttpResponseMessage := HttpClient.DeleteAsync(QueryUrl).Result;
END;
// Response auswerten:
//HttpResponseMessage.EnsureSuccessStatusCode(); // Throws an error when no success
//HttpResponseMessage.IsSuccessStatusCode; // True or false
// Response als String
Encoding := Encoding.GetEncoding(HttpResponseMessage.Content.Headers.ContentType.CharSet);
byteArray := HttpResponseMessage.Content.ReadAsByteArrayAsync().Result;
ResponseMessageString := Encoding.GetString(byteArray, 0, byteArray.Length);
// 1. Alternative (ungetestet)
ResponseMessageString := HttpResponseMessage.Content.ReadAsStringAsync().Result;
// 2. Alternative (ungetestet)
ResponseMessageString := HttpResponseMessage.ReasonPhrase;
Name DataType Subtype Length
Uri DotNet System.Uri.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
UriPartial DotNet System.UriPartial.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
HttpClient DotNet System.Net.Http.HttpClient.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
HttpContent DotNet System.Net.Http.HttpContent.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
HttpResponseMessage DotNet System.Net.Http.HttpResponseMessage.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
byteArray DotNet System.Array.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
Encoding DotNet System.Text.Encoding.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'