使用Twilio的卷曲与HttpClient [英] Using Twilio's curl with HttpClient

查看:79
本文介绍了使用Twilio的卷曲与HttpClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有来自Twilio的API,我想知道如何使用HttpClient来利用这个卷曲,以便我可以在我的应用程序中使用它?

I have below API from Twilio and I want to know how can utilize this curl using HttpClient so that I can use it in my app?

我也需要传递参数,例如  Body,From,To等..

I need to pass parameters too, e.g. Body, From, To, etc..

curl
'https://api.twilio.com/2010-04-01/Accounts/ ACxxxxxxxxxxxxxxxxx529d6d / Messages.json'
-X POST \ -u ACxxxxxxxxxxxxxxxxx529d6d:[AuthToken]

我尝试了下面的代码,但得到了:

I tried below code but getting:

/Users/jassim/Projects/TwilioTest/TwilioTest/TwilioTestPage.xaml.cs(62,62):错误CS1729:'AuthenticationHeaderValue'不包含带3个参数的构造函数(CS1729)(TwilioTest)

/Users/jassim/Projects/TwilioTest/TwilioTest/TwilioTestPage.xaml.cs(62,62): Error CS1729: 'AuthenticationHeaderValue' does not contain a constructor that takes 3 arguments (CS1729) (TwilioTest)

public async Task<HttpResponseMessage> SendReq()
{
    var accountSid = "zzzzzzzzzzz";
    var authToken = "xxxxxxxxxxx";
    var targeturi = "https://api.twilio.com/2010-04-01/Accounts/{0}/SMS/Messages";
    string from = "+0000000000";
    string to = "WoWSender";
    string message = "Test Message";

    var client = new System.Net.Http.HttpClient();
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", accountSid, authToken);
    // client.DefaultRequestHeaders.Authorization = CreateAuthenticationHeader("Basic", accountSid, authToken);

    var content = new StringContent(string.Format("From={0}&amp;To={1}&amp;Body={2}", from, to, message));
    content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");

    var response = await client.PostAsync(string.Format(targeturi, accountSid), content);
    if (response.IsSuccessStatusCode)
    {
        //the POST succeeded, so update the UI accordingly
    }
    else
    {
        //the POST failed, so update the UI accordingly
    }
    return response;
}

谢谢,

Jassim

推荐答案

嗨Jassim Rahma,

Hi Jassim Rahma,

谢谢你在这里发布。

对于您的问题,AuthenticationHeaderValue有两个构造函数。您使用的具有三个参数的构造函数不存在。

For your question, the AuthenticationHeaderValue has two constructors. The constructor with three parameters which you used does not exist.

以下是有关构造函数的msdn文档供您参考。

Here is the msdn document about the constructor for your reference.

https://开头msdn.microsoft.com/en-us/library/system.net.http.headers.authenticationheadervalue%28v=vs.118%29.aspx?f=255&MSPPError=-2147217396

以下是AuthenticationHeaderValue类的用法。将所有字符串放入一个参数中。请参阅链接中的示例。

Here is the usage of AuthenticationHeaderValue class. Put all the strings into one parameter. Please refer to the example in the link.

https://social.msdn.microsoft.com/Forums/en-US/6f2b2ce4-9b47-4c10-8941-1d9d2e56b0b9/how-to -use-authenticationheadervalue-class-?forum = winappswithcsharp

最好的问候,

Wendy


这篇关于使用Twilio的卷曲与HttpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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