C# 由于Web API中的特殊字符,未设置Post值

C# 由于Web API中的特殊字符,未设置Post值,c#,asp.net-mvc,asp.net-mvc-4,asp.net-web-api,C#,Asp.net Mvc,Asp.net Mvc 4,Asp.net Web Api,我正在尝试向我的web api服务发布帖子。关键是,在发送一条类似 { message: "it is done" } 工作很好。但是,当我在消息中使用特殊字符,如çıöpş时,它无法转换json,因此post对象保持空。我能做什么?这不是当前的文化问题,就是其他问题。我试图用编码HttpUtility类将我的post参数作为HtmlEncoded样式发送,但也没有成功 public class Animal{ public string Message {get;set;} } we

我正在尝试向我的web api服务发布帖子。关键是,在发送一条类似

{ message: "it is done" }
工作很好。但是,当我在消息中使用特殊字符,如çıöpş时,它无法转换json,因此post对象保持空。我能做什么?这不是当前的文化问题,就是其他问题。我试图用编码HttpUtility类将我的post参数作为HtmlEncoded样式发送,但也没有成功

public class Animal{

  public string Message {get;set;}
}
webapi方法

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);
致以最良好的祝愿


Kemal

一种可能性是使用允许您在编码数据时指定UTF-8的方法,因为您使用的
UploadStringAsync
方法基本上使用
编码。默认情况下,将数据写入套接字时对其进行编码。因此,如果您的系统配置为使用UTF-8以外的其他编码,则会遇到问题,因为
UploadStringAsync
使用您的系统编码,而在内容类型标头中,您指定了可能冲突的
charset=UTF-8

使用
UploadDataAsync
方法,您的意图可以更加明确:

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);
}

或者,如果您在客户端上安装了NuGet软件包,则可以直接使用新的
HttpClient
类(这是块中的新成员)来使用WebAPI,而不是
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();
}
动物a=新动物();
a、 Message=“öçşistltl”;
var URI=”http://localhost/Values/DoSomething";
使用(var client=new HttpClient())
{
客户
.PostAsync(URI,新的JsonMediaTypeFormatter())
.ContinueWith(x=>x.Result.Content.ReadAsStringAsync().ContinueWith(y=>
{
控制台写入线(y.Result);
}))
.Wait();
}

请显示您的代码。最重要的是,它将请求发送到Web API。您的Web API操作也会有所帮助。你怎么可能期望我们告诉你为什么你的代码不显示就不能工作?这太荒谬了。我编辑了我的评论,希望这有帮助。你试过
client.Encoding=Encoding.UTF8?使用fiddler查看正在发布的实际消息。JsonConvert是在开源程序集中定义的吗?我在谷歌上搜索了一下,发现了一些Newtonsoft.Json程序集。或者JsonConvert是在Microsoft提供的程序集中定义的?它是开源Newtonsoft.Json包的一部分,Microsoft现在正随Web API一起分发。