我在C#中有一个Windows phone8项目,我正在使用RestSharp 104.4.0向服务器发出请求。服务器只接受"application/json“的"Accept”类型。调用该请求的代码:
var client = new RestClient(_jsonBaseUrl + someURL)
{
Authenticator = new HttpBasicAuthenticator(someUsername, somePassword)
};
var request = new RestRequest(Method.POST);
UTF8Encoding utf8 = new UTF8Encoding();
byte[] bytes = utf8.GetBytes(json);
json = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
request.RequestFormat = DataFormat.Json;
request.AddHeader("Accept", "application/json");
request.AddBody(json);
request.Parameters.Clear();
request.AddParameter("application/json", json, ParameterType.RequestBody);
client.ExecuteAsync<UserAccount>(request, response =>
{
if (response.ResponseStatus == ResponseStatus.Error)
{
failure(response.ErrorMessage);
}
else
{
success("done");
}
});" JSON“变量是一个JSON字符串。
正如您所看到的,我已经将我的接受类型设置为AddHeader(" accept ","application/json");但是由于某种有线原因,服务器将其作为接受类型接收:" accept ":"application/json,application/xml,text/json,text/x-json,text/javascript,text/xml“
要确保服务器得到的唯一接受类型是" accept ":"application/json“,我必须做什么?
任何形式的帮助都将不胜感激。
发布于 2014-03-07 00:51:10
来自https://groups.google.com/forum/#!topic/restsharp/KD2lsaTC0eM
通过检查注册到RestClient实例的“处理程序”,
自动生成Accept标头。一种选择是使用RestClient.ClearHandlers ();,然后用RestClient.AddHandler(“RestClient.AddHandler/ JSON”,new JsonDeserializer ());
显式地添加回json反序列化器
https://stackoverflow.com/questions/22229393
复制相似问题