gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:43:53 24 4
gpt4 key购买 nike

我正在尝试将一些 JSON 信息发送到服务器。我将预序列化字符串添加到正文中,并将一些属性放置到 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);

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

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

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

          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
}

我假设“日期”是 Rest Sharp 预定义或预填充的值。

我添加了一个 dateFormat 定义

          request.DateFormat = "MMMM dd, yyyy";

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

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

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

最佳答案

似乎某些属性如content-typedate 不能作为参数添加,而是在内部添加。要更改“内容类型”的值,我必须更改序列化器设置(尽管我没有使用它,因为我在之前序列化的正文中添加了一个 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";

一旦我这样做,标题就会按预期显示:

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
...
}

关于c# - 无法在 Rest 请求中添加日期参数(Rest Sharp),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29080778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com