后没有设置值,因为在网页API的特殊字符 [英] Post values are not set because of special characters in Web API

查看:223
本文介绍了后没有设置值,因为在网页API的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个帖子到我的网页API服务。的一点是,在发送这样的消息

I'm trying to make a post to my web api service. The point is that while sending a message like

{ message: "it is done" }

工作的罚款。然而,当我在消息中使用特殊字符,如çıöpş它无法使后对象保持零到我的JSON的转换。我能做什么?它可以是当前文化问题还是其他什么东西。我想送我的帖子参数作为HtmlEn codeD风格与编码HttpUtility类,但是它也不能工作。

working fine. However when I use special characters like çıöpş in my message it is unable to convert my json so that the post object remains null. What can I do? It is either current culture issue or something else. I tried to send my post parameter as HtmlEncoded style with encoding HttpUtility class but it didn't work either.

public class Animal{

  public string Message {get;set;}
}

网页API方法

public void DoSomething(Animal a){

}

客户端

Animal a = new Animal();
a.Message = "öçşistltl";
string postDataString = JsonConvert.SerializeObject(a);        
string URL = "http://localhost/Values/DoSomething";
WebClient client = new WebClient();

client.UploadStringCompleted += client_UploadStringCompleted;
client.Headers["Content-Type"] = "application/json;charset=utf-8";
client.UploadStringAsync(new Uri(URL), "POST",postDataString);

最好的问候,

凯末尔

推荐答案

一种可能是使用的 UploadDataAsync 方法,它允许您指定UTF-8编码数据,因为您正在使用基本 UploadStringAsync 方法使用 Encoding.Default 为en code将其写入套接字时的数据。所以,如果你的系统配置为使用其他的编码不是UTF-8你遇到麻烦,因为 UploadStringAsync 使用系统的编码,而在你的内容类型头已指定的charset = UTF-8 这可能是相互冲突的。

One possibility is to use the UploadDataAsync method which allows you to specify UTF-8 when encoding the data because the UploadStringAsync method that you are using basically uses Encoding.Default to encode the data when writing it to the socket. So if your system is configured to use some other encoding than UTF-8 you get into trouble because UploadStringAsync uses your system encoding whereas in your content type header you have specified charset=utf-8 which could be conflicting.

随着 UploadDataAsync 方法,你可以在你的意图更为明确:

With the UploadDataAsync method you could be more explicit in your intents:

Animal a = new Animal();
a.Message = "öçşistltl";
string postDataString = JsonConvert.SerializeObject(a);        
string URL = "http://localhost/Values/DoSomething";
string postDataString = JsonConvert.SerializeObject(a);
using (WebClient client = new WebClient())
{
    client.UploadDataCompleted += client_UploadDataCompleted;
    client.Headers["Content-Type"] = "application/json; charset=utf-8";
    client.UploadDataAsync(new Uri(URI), "POST", Encoding.UTF8.GetBytes(postDataString));
}


另一种可能性是指定客户端的编码,并使用 UploadStringAsync

Animal a = new Animal();
a.Message = "öçşistltl";
string postDataString = JsonConvert.SerializeObject(a);        
string URL = "http://localhost/Values/DoSomething";
string postDataString = JsonConvert.SerializeObject(a);
using (WebClient client = new WebClient())
{
    client.Encoding = Encoding.UTF8;
    client.UploadStringCompleted += client_UploadStringCompleted;
    client.Headers["Content-Type"] = "application/json; charset=utf-8";
    client.UploadStringAsync(new Uri(URI), "POST", postDataString);
}


或者,如果你安装了 Microsoft.AspNet.WebApi.Client 你可以直接使用新的的HttpClient 类(这是新来的小子块上),消费客户端上的NuGet包你的WebAPI而不是 Web客户端


Or if you install the Microsoft.AspNet.WebApi.Client NuGet package on the client you could directly use the new HttpClient class (which is the new kid on the block) to consume your WebAPI instead of WebClient:

Animal a = new Animal();
a.Message = "öçşistltl";
var URI = "http://localhost/Values/DoSomething";
using (var client = new HttpClient())
{
    client
        .PostAsync<Animal>(URI, a, new JsonMediaTypeFormatter())
        .ContinueWith(x => x.Result.Content.ReadAsStringAsync().ContinueWith(y =>
        {
            Console.WriteLine(y.Result);
        }))
        .Wait();
}

这篇关于后没有设置值,因为在网页API的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆