无法在 Rest 请求中添加日期参数(Rest Sharp)

Cannot add date parameter in Rest request (Rest Sharp)

我正在尝试向服务器发送一些 JSON 信息。我将预序列化字符串添加到 body 并将一些属性添加到 header.

            RestClient client = new RestClient(requURI);
            RestRequest request = new RestRequest(reqPath, method);
            request.RequestFormat = DataFormat.Json;
            request.JsonSerializer.ContentType = "application/json; charset=utf-8";

            request.AddHeader("Date", getIsoStringFromDate(DateTime.Now));
            request.AddParameter("application/json; charset=utf-8", JSonString, ParameterType.RequestBody);

除了日期 Header 不会显示外,一切正常。当我将行更改为

           request.AddHeader("Datexxx", getIsoStringFromDate(DateTime.Now));

它将显示在 header 中(参见网络跟踪)

          System.Net Information: 0 : [5620] ConnectStream#61150033 -   Header 
          {
           Datexxx: 2015-03-16 16:19:39
           Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml
           User-Agent: RestSharp 104.1.0.0
           Content-Type: application/json; charset=utf-8
           Host: localhost:8080
           Content-Length: 620
           Accept-Encoding: gzip, deflate
           Connection: Keep-Alive
          }

我假设 "date" 是 Rest Sharp 预定义或预填充的值。

我添加了一个日期格式定义

          request.DateFormat = "MMMM dd, yyyy";

但是仍然没有日期 header 属性出现。还尝试在添加之前清除所有参数,但也无济于事。

有一个与此相关的 github 错误,但它已有 2 年多的历史了..也许我只是缺少 "includeDateInHeader" 开关之类的东西。

我使用 .Net 3.5 和 Rest Sharp 104.1.0.0。或 105.1.0.0。提前致谢!

似乎某些属性如content-typedate不能作为参数添加,而是在内部添加。要更改 "content-type" 的值,我必须更改序列化程序设置(尽管我没有使用它,因为我在之前序列化的 body 中添加了一个 json!)

RestClient client = new RestClient(requURI);
RestRequest request = new RestRequest(reqPath, method);
request.RequestFormat = DataFormat.Json;
request.Parameters.Clear();             
request.AddHeader("Date", getIsoStringFromDate(DateTime.Now));
            request.JsonSerializer.ContentType = "application/json; charset=utf-8";

我一这样做,header 就按预期出现了:

System.Net Information: 0 : [5620] ConnectStream#61150033 -   Header 
{
 Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml
 User-Agent: RestSharp 104.1.0.0
 Content-Type: application/json; charset=utf-8
 ...
}