如何使用C#调用web api [英] How to call web api using C#

查看:528
本文介绍了如何使用C#调用web api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用ExpertTexting Api发送短信,但是当我尝试使用post show show invalidnternter发送消息时,我的get方法正常工作。以下是我的帖子方法



私有静态字符串BaseUrl =https://www.experttexting.com/ExptRestApi/sms/json//Message/Send;

var data =?username =+ username +& password =+ password +& api_key =+ apikey +& from =+ from +& to =使用(var client = new HttpClient())

{

var serializedProduct = JsonConvert.SerializeObject(data);

var content = new StringContent(serializedProduct,Encoding.UTF8,application / json) ;

response = await client.PostAsJsonAsync(BaseUrl,content);

}



什么我试过了:



私有静态字符串BaseUrl =https://www.experttexting.com/ExptRestApi/sms/json//Message/Send ;

var data =?username =+ username +& password =+ password +& api_key =+ apikey + & from =+ from +& to =+ to +& text =+ text +& type = text;



使用(var client = new HttpClient())

{

var serializedProduct = JsonConvert.SerializeObject(data);

var content = new StringContent(serializedProduct,Encoding.UTF8,application / json);

response = await client.PostAsJsonAsync(BaseUrl,content);

}

i am calling ExpertTexting Api for sending SMS but my get Method is working fine when i try to send message using post its show invalid paramenter. below is my post method

private static string BaseUrl = "https://www.experttexting.com/ExptRestApi/sms/json//Message/Send";
var data = "?username=" + username + "&password=" + password + "&api_key=" + apikey + "&from=" + from + "&to=" + to + "&text=" + text + "&type=text";

using (var client = new HttpClient())
{
var serializedProduct = JsonConvert.SerializeObject(data);
var content = new StringContent(serializedProduct, Encoding.UTF8, "application/json");
response = await client.PostAsJsonAsync(BaseUrl, content);
}

What I have tried:

private static string BaseUrl = "https://www.experttexting.com/ExptRestApi/sms/json//Message/Send";
var data = "?username=" + username + "&password=" + password + "&api_key=" + apikey + "&from=" + from + "&to=" + to + "&text=" + text + "&type=text";

using (var client = new HttpClient())
{
var serializedProduct = JsonConvert.SerializeObject(data);
var content = new StringContent(serializedProduct, Encoding.UTF8, "application/json");
response = await client.PostAsJsonAsync(BaseUrl, content);
}

推荐答案

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

  • 数据字符串包含 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"
        });
    } 


    这篇关于如何使用C#调用web api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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