diff --git a/ocaml/sdk-gen/csharp/autogen/src/JsonRpc.cs b/ocaml/sdk-gen/csharp/autogen/src/JsonRpc.cs index 4b36412271..71c9ea81f4 100644 --- a/ocaml/sdk-gen/csharp/autogen/src/JsonRpc.cs +++ b/ocaml/sdk-gen/csharp/autogen/src/JsonRpc.cs @@ -181,6 +181,8 @@ public JsonRpcClient(string baseUrl) public bool PreAuthenticate { get; set; } public CookieContainer Cookies { get; set; } public RemoteCertificateValidationCallback ServerCertificateValidationCallback { get; set; } + public Dictionary RequestHeaders { get; set; } + public Dictionary ResponseHeaders { get; set; } public string Url { get; private set; } @@ -285,14 +287,32 @@ protected virtual void PerformPostRequest(Stream postStream, Stream responseStre webRequest.CookieContainer = Cookies ?? webRequest.CookieContainer ?? new CookieContainer(); webRequest.ServerCertificateValidationCallback = ServerCertificateValidationCallback ?? ServicePointManager.ServerCertificateValidationCallback; + if (RequestHeaders != null) + { + foreach (var header in RequestHeaders) + webRequest.Headers.Add(header.Key, header.Value); + } + using (var str = webRequest.GetRequestStream()) { postStream.CopyTo(str); str.Flush(); } - using (var webResponse = (HttpWebResponse)webRequest.GetResponse()) + HttpWebResponse webResponse = null; + try { + webResponse = (HttpWebResponse)webRequest.GetResponse(); + + ResponseHeaders = new Dictionary(); + + if (webResponse.Headers != null) + { + var keys = webResponse.Headers.AllKeys; + foreach (var key in keys) + ResponseHeaders.Add(key, string.Join(",", webResponse.Headers.Get(key))); + } + if (webResponse.StatusCode != HttpStatusCode.OK) throw new WebException(webResponse.StatusCode.ToString()); @@ -305,6 +325,11 @@ protected virtual void PerformPostRequest(Stream postStream, Stream responseStre responseStream.Flush(); } } + finally + { + RequestHeaders = null; + webResponse?.Dispose(); + } } private JsonSerializerSettings CreateSettings(IList converters) diff --git a/ocaml/sdk-gen/csharp/autogen/src/Session.cs b/ocaml/sdk-gen/csharp/autogen/src/Session.cs index d78915a902..a2aef1d672 100644 --- a/ocaml/sdk-gen/csharp/autogen/src/Session.cs +++ b/ocaml/sdk-gen/csharp/autogen/src/Session.cs @@ -279,6 +279,25 @@ public RemoteCertificateValidationCallback ServerCertificateValidationCallback public ICredentials Credentials => JsonRpcClient?.WebProxy?.Credentials; + /// + /// Optional headers in name-value pairs to be passed in the HttpWebRequests. The + /// default value is null. This property can be set by the implementing code before + /// each request. It is automatically reset to null once the request has been sent. + /// + public Dictionary RequestHeaders + { + set => JsonRpcClient.RequestHeaders = value; + get => JsonRpcClient.RequestHeaders; + } + + /// + /// Exposes the headers returned in the HttpWebResponses in name-value pairs. + /// This property is set once a response is received. The values are comma + /// separated strings of header values stored in a header. + /// It returns an empty dictionary if no headers are found. + /// + public Dictionary ResponseHeaders => JsonRpcClient.ResponseHeaders; + /// /// Always true before API version 1.6. /// Filled in after successful session_login_with_password for 1.6 or newer connections