1
我想從C#單元測試代碼發送短信,但我沒能收到短信,我不知道在哪裏可以調試此。發送短信使用消息twilio不工作
而且,我的反應對象的內部,我得到的值「錯誤的請求」。我究竟做錯了什麼。 也在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;
}
我編輯我的問題。 –
@ DOTNET-從業者從[twilio文檔】(http://www.twilio.com/docs/api/rest/request),錯誤的請求的意思是「在給定的POST或PUT驗證失敗的數據,檢查響應主體瞭解詳情。「看看這個響應組織,讓我知道你是否還有問題。而擺脫'do..while'循環,這不是做你任何好處:你以後再'await'或做'.Result',要求做,並沒有什麼都不會改變。 –
@ dotnet-practitioner我已經添加了一個使用FormUrlEncodedContent的建議。這會產生與你的'StringContent'代碼不同的請求。這可能是您的「錯誤請求」錯誤的原因。 –