我正在尝试从 c# 单元测试代码发送 SMS 消息,但我无法接收到该文本消息,并且我不知道在哪里进行调试。
此外,在我的响应对象中,我得到值“错误请求”。我究竟做错了什么。同样在 while 循环中,我等待响应得到处理。
这是我的代码。
[TestMethod]
public void TestMethod1()
{
Assert.IsTrue(SendMessage("+1MYFROMPHONENUMBER", "+1MYTOPHONENUMBER", "sending from code"));
}
public bool SendMessage(string from, string to, string message)
{
var accountSid = "MYACCOUNTSIDFROMTWILIOACCOUNT";
var authToken = "MYAUTHTOKENFROMTWILIOACCOUNT";
var targeturi = "https://api.twilio.com/2010-04-01/Accounts/{0}/SMS/Messages";
var client = new System.Net.Http.HttpClient();
client.DefaultRequestHeaders.Authorization = CreateAuthenticationHeader("Basic", accountSid, authToken);
var content = new StringContent(string.Format("From={0}&To={1}&Body={2}", from, to, message));
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
var result = false;
var response = client.PostAsync(string.Format(targeturi, accountSid), content).Result;
do
{
result = response.IsSuccessStatusCode;
} while (result == false);
return result;
}