且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用C#调用web api

更新时间:2022-11-09 09:32:37

您混合了多种发送数据的方式:

  • 数据字符串包含 application / x-www-form-urlencoded data:
You're mixing up multiple ways of sending data:
  • The data string contains application/x-www-form-urlencoded data:
?username=...&type=text

  • 然后调用 JsonConvert.SerializeObject 将该字符串转换为JSON:

  • You then call JsonConvert.SerializeObject to convert that string to JSON:
    "?username=...&type=text"

  • 你然后调用 PostAsJsonAsync ,它将再次对数据进行JSON编码
    "\"?username=...&type=text\""

  • using (var client = new HttpClient())
    {
        response = await client.PostAsJsonAsync(BaseUrl, new
        {
            username,
            password,
            api_key = apikey,
            from,
            to,
            text,
            type = "text"
        });
    }