|
@@ -262,6 +262,61 @@ namespace EVCB_OCPP.WSServer.Service
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ public virtual async Task<HttpResponse> PostFormDataAsync(string Url, Dictionary<string, string> bodyData, Dictionary<string, string> headers, string clientName = "Default", bool bearerToken = false, string authorizationToken = null)
|
|
|
+ {
|
|
|
+ HttpResponse result = new HttpResponse() { IsError = false };
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var client = _clientFactory.CreateClient(clientName);
|
|
|
+
|
|
|
+ ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
|
|
|
+
|
|
|
+ if (!string.IsNullOrEmpty(authorizationToken))
|
|
|
+ {
|
|
|
+ client.DefaultRequestHeaders.Authorization = bearerToken ? new AuthenticationHeaderValue("Bearer", authorizationToken) : new AuthenticationHeaderValue(authorizationToken);
|
|
|
+ }
|
|
|
+ if (headers != null)
|
|
|
+ {
|
|
|
+ for (int idx = 0; idx < headers.Count; idx++)
|
|
|
+ {
|
|
|
+ client.DefaultRequestHeaders.Add(headers.ElementAt(idx).Key, headers.ElementAt(idx).Value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var content = new MultipartFormDataContent();
|
|
|
+
|
|
|
+ foreach (var keyValuePair in bodyData)
|
|
|
+ {
|
|
|
+ content.Add(new StringContent(keyValuePair.Value), "\"" + keyValuePair.Key + "\"");
|
|
|
+ }
|
|
|
+
|
|
|
+ var response = await client.PostAsync(Url, content).ConfigureAwait(false);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ result.IsSuccessStatusCode = response.IsSuccessStatusCode;
|
|
|
+ result.Headers = response.Headers;
|
|
|
+ result.RequestMessage = response.RequestMessage;
|
|
|
+ result.StatusCode = response.StatusCode;
|
|
|
+ result.Response = await response.Content.ReadAsStringAsync();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ result.IsError = true;
|
|
|
+ result.Exception = ex;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|