我正在尝试与REST服务交谈,使用Postman或Fiddler等工具,我得到了预期的结果。
现在我正在尝试使用RestSharp 104.4从我的C#代码中执行同样的操作。
我设置了REST客户端并定义了用户代理字符串和默认值
Content-Type
和
Accept
标题:
_restClient = new RestClient();
_restClient.UserAgent = CreateUserAgentString();
_restClient.AddDefaultHeader("Content-Type", "application/json");
_restClient.AddDefaultHeader("Accept", "application/json");
然后我尝试调用一个名为
/token
要获取基于HTTP Basic Auth的安全令牌,请执行以下操作:
RestRequest request = new RestRequest(_baseServiceUrl + GetTokenUrl, Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddHeader("Authorization", "Basic " + baseAuth); // baseAuth contains the base Auth string
request.AddBody(request.JsonSerializer.Serialize(new { clientIdentifier = "Outlook", clientId = "52c600f6-262c-4fca-a4bc-e0e322e0571e" }));
我的问题是:产生的POST请求总是失败,并出现“内部服务器错误”:
Authorization: Basic .........
Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml
User-Agent: my defined user agent string
Content-Type: application/json
Host: dev.xxx.yyyyyyyy.ch
Content-Length: 90
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
正文:
"{\"clientIdentifier\":\"Outlook\",\"clientId\":\"52c600f6-262c-4fca-a4bc-e0e322e0571e\"}"
问题有几个:
-
我得到一个
接受
标题有太多的项目-我怎么才能得到
Accept: application/json
什么都没有?(正如我认为在REST客户端上设置默认标头可以做到的那样)
-
设置
Accept-Encoding: gzip,deflate
和
Connection: Keep-Alive
也似乎会带来麻烦-我需要在RESTSharp客户端上使用哪些选项/设置来避免这些问题?
-
POST主体没有必要
"
和
\
这会导致问题-我该如何解决?