gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:26:32 26 4
gpt4 key购买 nike

我在 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/

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