c# - 为什么是 RestSharp AddHeader ("Accept", "application/json"); = 到项目列表?

标签 c# json windows-phone-8 windows-phone restsharp

我在 C# 中有这个 Windows Phone 8 项目,我在其中使用 RestSharp 104.4.0 向服务器发出请求。服务器只接受“应用程序/json”的“接受”类型。我调用请求的代码:

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");但是由于某些有线原因,服务器将其接收为接受类型: "接受": "application/json, application/xml, text/json, text/x-json, text/javascript, text/xml"

我必须做什么,才能确保服务器获得的唯一接受类型是“Accept”:“application/json”?

如有任何帮助,我们将不胜感激。

最佳答案

来自 https://groups.google.com/forum/#!topic/restsharp/KD2lsaTC0eM :

The Accept header is automatically generated by inspecting the "handlers" that are registered with the RestClient instance. One optin is to use RestClient.ClearHandlers (); and then add back explicitly the JSON deserializer with RestClient.AddHandler("application/json", new JsonDeserializer ());

关于c# - 为什么是 RestSharp AddHeader ("Accept", "application/json"); = 到项目列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22229393/

相关文章:

xaml - 如何将位图图像更改为矢量路径

unit-testing - Windows Phone开发和单元测试

c# - 在 C# 中是否可以直接读取属性(而不是循环访问 PropertyNames)

c# - 递归创建 TreeView

c# - 在线程中设置全局变量 - C#

c# - 从 JSON 发布反序列化 int 数组

java - Jersey/MOXy 的 JSON 反序列化失败(HTTP 400)

json - 如何在java中解析下面的Json文件,该文件没有 key

windows - 类型或命名空间名称 'MD5CryptoServiceProvider' 不存在

c# - wpf为按钮默认样式添加样式属性