JSON数据传递到的WebAPI有特殊字符结果为空 [英] Passing json data to a WebApi with special characters results to null

查看:442
本文介绍了JSON数据传递到的WebAPI有特殊字符结果为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有被传递到的WebAPI JSON字符串,现在的问题是,当我尝试添加特殊字符,则recieving对象变为零。

I have a json string that is being passed to a webapi, now the problem is, when I try adding special characters, the recieving object becomes null.

下面是我做的。

string json = JsonConvert.SerializeObject(ojectParams);

        WebClient client = new WebClient();

        client.Headers.Add("content-type", "application/json; charset=utf-8");
        client.Headers.Add("AppKey", WebUser.AppKey);
        client.Headers.Add("AppSecret", WebUser.AppSecret);
        client.Headers.Add("AccountId", WebUser.AccountId.ToString());

        if (!string.IsNullOrEmpty(WebUser.StoreId))
        {
            client.Headers.Add("StoreId", WebUser.StoreId);
        }

        var returnedStringObject = client.UploadString(string.Format("{0}/{1}", ConfigurationManager.AppSettings["Api"], endpoint), method, json);

下面是JSON字符串:

Here's the json string:

"{\"Firstname\":\"kyv®\",\"Lastname\":\"sab®\"}"

我加入这一项上的头希望它能够解决这个问题。但是与没有运气。

I have added this one on the header hoping that it will fix the issue. But no luck with that.

charset=utf-8

在recieving端点,与obj变空。但是,当我删除了特殊字符,该值被传递。

On the recieving endpoint, the obj becomes null. But when I removed the special characters, the value is being passed.

[HttpPost]
public responseObj Endpoint(requestObj request)

任何想法?谢谢!

Any ideas? Thanks!

推荐答案

您需要设置Web客户端的编码

You need to set the Encoding of the WebClient

client.Encoding = Encoding.UTF8;

这篇关于JSON数据传递到的WebAPI有特殊字符结果为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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