1

我之前已经成功地用 C# 实现了 Twilio REST API。然而,突然之间,API 调用不断收到 400 - BAD REQUEST。

响应正文也不包含任何特定错误...

我再说一遍,它工作了一两个星期,突然间它返回了 BAD REQUEST。

代码如下。

public async Task SendSmsAsync(string number, string message)
{
    var accountSid = _configuration["Authentication:Twilio:AccountSID"];
    var authToken = _configuration["Authentication:Twilio:AuthToken"];
    var twilioNumber = _configuration["Authentication:Twilio:Number"];

    var credentials = new NetworkCredential(accountSid, authToken);
    var handler = new HttpClientHandler { Credentials = credentials };

    using (var client = new HttpClient(handler))
        {
            var url = $"https://api.twilio.com/2010-04-01/Accounts/{ accountSid }/Messages";
            var body = new List<KeyValuePair<string, string>>
            {
                new KeyValuePair<string, string>("To", number),
                new KeyValuePair<string, string>("From", twilioNumber),
                new KeyValuePair<string, string>("Body", message),
            };

            var content = new FormUrlEncodedContent(body);
            content.Headers.ContentType.CharSet = "UTF-8";

            var response = await client.PostAsync(url, content);
            if (response.IsSuccessStatusCode)
            {
                //Uri success = response.Headers.Location;
            }
       }
}
4

0 回答 0