【问题标题】:How to add parameter to api request using WebClient.UploadString in C#如何在 C# 中使用 WebClient.UploadString 向 api 请求添加参数
【发布时间】:2014-02-13 14:03:50
【问题描述】:

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

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

【问题讨论】:

  • 您的 JSON 请求正文。显然不能随意修改,必须还是JSON。您确定您对 API 对您的期望有正确的认识吗?
  • API 需要定义一个参数的 JSON 格式请求。我不知道如何将参数添加到请求正文中。我是 apis 新手,如果我遗漏了什么,请见谅。

标签: c# json api post uploadstring


【解决方案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);
}

【讨论】:

    猜你喜欢
    • 2016-04-13
    • 2019-09-24
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    相关资源
    最近更新 更多