Skip to content

Commit 3233a76

Browse files
author
Konstantina Chremmou
committed
CP-308539 Use HttpClient for .NET as HttpWebRequest is obsolete.
Signed-off-by: Konstantina Chremmou <[email protected]>
1 parent 3dc18af commit 3233a76

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

ocaml/sdk-gen/csharp/autogen/src/JsonRpc.cs

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
using System.Collections.Generic;
3232
using System.IO;
3333
using System.Net;
34+
#if (NET8_0_OR_GREATER)
35+
using System.Linq;
36+
using System.Net.Http;
37+
using System.Net.Http.Headers;
38+
using System.Security.Cryptography.X509Certificates;
39+
#endif
3440
using System.Net.Security;
3541
using System.Threading;
3642
using Newtonsoft.Json;
@@ -177,7 +183,13 @@ public JsonRpcClient(string baseUrl)
177183
public bool AllowAutoRedirect { get; set; }
178184
public bool PreAuthenticate { get; set; }
179185
public CookieContainer Cookies { get; set; }
186+
187+
#if (NET8_0_OR_GREATER)
188+
public Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool> ServerCertificateValidationCallback { get; set; }
189+
#else
180190
public RemoteCertificateValidationCallback ServerCertificateValidationCallback { get; set; }
191+
#endif
192+
181193
public Dictionary<string, string> RequestHeaders { get; set; }
182194
public Dictionary<string, string> ResponseHeaders { get; set; }
183195

@@ -264,9 +276,65 @@ protected virtual T Rpc<T>(string callName, JToken parameters, JsonSerializer se
264276
}
265277
}
266278

267-
268279
protected virtual void PerformPostRequest(Stream postStream, Stream responseStream)
269280
{
281+
#if (NET8_0_OR_GREATER)
282+
HttpClient httpClient = null;
283+
HttpClientHandler httpHandler = null;
284+
HttpRequestMessage requestMessage = null;
285+
HttpResponseMessage responseMessage = null;
286+
287+
try
288+
{
289+
httpHandler = new HttpClientHandler
290+
{
291+
AllowAutoRedirect = AllowAutoRedirect,
292+
PreAuthenticate = PreAuthenticate,
293+
CookieContainer = Cookies ?? new CookieContainer(),
294+
Proxy = WebProxy
295+
};
296+
297+
Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool> callBack = null;
298+
if (ServicePointManager.ServerCertificateValidationCallback != null)
299+
callBack = ServicePointManager.ServerCertificateValidationCallback.Invoke;
300+
301+
httpHandler.ServerCertificateCustomValidationCallback = ServerCertificateValidationCallback ?? callBack;
302+
303+
httpClient = new HttpClient(httpHandler) { Timeout = TimeSpan.FromMilliseconds(Timeout) };
304+
305+
requestMessage = new HttpRequestMessage(HttpMethod.Post, new Uri(JsonRpcUrl));
306+
if (ProtocolVersion != null)
307+
requestMessage.Version = ProtocolVersion;
308+
requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
309+
requestMessage.Headers.UserAgent.ParseAdd(UserAgent);
310+
requestMessage.Headers.ConnectionClose = !KeepAlive;
311+
requestMessage.Headers.ExpectContinue = Expect100Continue;
312+
requestMessage.Content = new StreamContent(postStream);
313+
314+
if (RequestHeaders != null)
315+
{
316+
foreach (var header in RequestHeaders)
317+
requestMessage.Headers.Add(header.Key, header.Value);
318+
}
319+
320+
responseMessage = httpClient.SendAsync(requestMessage).Result;
321+
responseMessage.EnsureSuccessStatusCode();
322+
323+
var str = responseMessage.Content.ReadAsStream();
324+
str.CopyTo(responseStream);
325+
responseStream.Flush();
326+
327+
ResponseHeaders = responseMessage.Headers.ToDictionary(header => header.Key, header => string.Join(",", header.Value));
328+
}
329+
finally
330+
{
331+
RequestHeaders = null;
332+
responseMessage?.Dispose();
333+
requestMessage?.Dispose();
334+
httpClient?.Dispose();
335+
httpHandler?.Dispose();
336+
}
337+
#else
270338
var webRequest = (HttpWebRequest)WebRequest.Create(JsonRpcUrl);
271339
webRequest.Method = "POST";
272340
webRequest.ContentType = "application/json";
@@ -329,6 +397,7 @@ protected virtual void PerformPostRequest(Stream postStream, Stream responseStre
329397
RequestHeaders = null;
330398
webResponse?.Dispose();
331399
}
400+
#endif
332401
}
333402

334403
private JsonSerializerSettings CreateSettings(IList<JsonConverter> converters)

ocaml/sdk-gen/csharp/autogen/src/Session.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
using System.Collections.Generic;
3232
using System.Linq;
3333
using System.Net;
34+
#if (NET8_0_OR_GREATER)
35+
using System.Net.Http;
36+
using System.Security.Cryptography.X509Certificates;
37+
#endif
3438
using System.Net.Security;
3539
using Newtonsoft.Json;
3640

@@ -248,11 +252,19 @@ public int Timeout
248252
set => JsonRpcClient.Timeout = value;
249253
}
250254

255+
#if (NET8_0_OR_GREATER)
256+
public Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool> ServerCertificateValidationCallback
257+
{
258+
get => JsonRpcClient?.ServerCertificateValidationCallback;
259+
set => JsonRpcClient.ServerCertificateValidationCallback = value;
260+
}
261+
#else
251262
public RemoteCertificateValidationCallback ServerCertificateValidationCallback
252263
{
253264
get => JsonRpcClient?.ServerCertificateValidationCallback;
254265
set => JsonRpcClient.ServerCertificateValidationCallback = value;
255266
}
267+
#endif
256268

257269
public ICredentials Credentials => JsonRpcClient?.WebProxy?.Credentials;
258270

0 commit comments

Comments
 (0)