use*_*952 5 c# json restsharp windows-phone windows-phone-8
我在C#中有这个Windows Phone 8项目,我正在使用RestSharp 104.4.0向服务器发出请求.服务器只接受"Accept"类型的"application/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");
}
});
Run Code Online (Sandbox Code Playgroud)
"json"变量是一个JSON字符串.
如你所见,我已将我的接受类型设置为AddHeader("Accept","application/json"); 但由于一些有线的原因,服务器接收它作为接受类型:"接受":"application/json,application/xml,text/json,text/x-json,text/javascript,text/xml"
我需要做什么,以确保服务器获得的唯一接受类型是"接受":"application/json"?
任何形式的帮助将不胜感激.
Ton*_*ina 10
来自https://groups.google.com/forum/#!topic/restsharp/KD2lsaTC0eM:
通过检查向RestClient实例注册的"处理程序"自动生成Accept标头.一个选择是使用RestClient.ClearHandlers(); 然后使用RestClient.AddHandler("application/json",new JsonDeserializer())显式添加JSON反序列化器;
| 归档时间: |
|
| 查看次数: |
11217 次 |
| 最近记录: |