我正在尝试将一些JSON信息发送到服务器。我在主体上添加了一个预序列化的字符串,并在标头中放置了一些属性。
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-type或date)无法添加为参数,而是在内部添加。要更改“ content-type”的值,我必须更改serialzer设置(尽管我没有使用它,因为我在之前进行序列化的主体中添加了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
...
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句