如何在C#中使用WebClient.UploadString向api请求添加参数

时间:2014-02-13 14:03:50

标签: c# json api post uploadstring

我使用WebClient.UploadString方法向Web api发布JSON请求。通过将文本文件解析为JSON格式的字符串来创建JSON字符串。如何使用下面的代码在请求正文中定义参数?看起来我只是将它添加到我的jsonPOSTString字符串变量的末尾?

string result = "";
    using (var client = new WebClient())
    {
        result = client.UploadString(url, "POST", jsonPOSTString);
    }

1 个答案:

答案 0 :(得分:1)

你不知道这段代码是做什么的,但我认为你会从服务器收到错误,因为你没有supply the content-type

string result;
using (var client = new WebClient())
{
    client.Headers.Add("Content-Type","application/json");
    result = client.UploadString(url, "POST", jsonPOSTString);
}