我有一个传递给 webapi 的 json 字符串,现在的问题是,当我尝试添加特殊字符时,接收对象变为空。
这是我做的。
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字符串:
"{\"Firstname\":\"kyv®\",\"Lastname\":\"sab®\"}"
我在标题上添加了这个,希望它能解决这个问题。但没有运气。
charset=utf-8
在接收端点上,obj 变为 null。但是当我删除特殊字符时,正在传递该值。
[HttpPost]
public responseObj Endpoint(requestObj request)
有任何想法吗?谢谢!
最佳答案
您需要设置 WebClient 的编码
client.Encoding = Encoding.UTF8;
关于json - 将 json 数据传递给带有特殊字符的 Web Api 结果为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23760824/