1

I am trying to send SMS message from c# unit test code but I am not able to receive the text message and I don't know where to debug this.

Also, inside my response object, I get the value "Bad Request". What am I doing wrong. Also in while loop, I wait for the response to get processed.

Here is my code.

   [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-practitioner
  • 13,968
  • 36
  • 127
  • 200

1 Answers1

4

You're making an asynchronous request, but not waiting for the result. Try:

var response = client.PostAsync(..).Result;
return response.IsSuccessStatusCode;

Also, you should be able to format your parameters more easily/safely using the FormUrlEncodedContent type, instead of StringContent. It appears to be made to support what you're trying to do. E.g.

var dict = new Dictionary<string, string>
    { { "From", from }, { "To", to }, { "Body", message } };
var content = new FormUrlEncodedContent(dict);

And since this creates a different request body than your content, this might be the cause of your "Bad Request" error. The two ways of doing this encode special characters, both the &'s separating the portions and the strings themselves, quite differently. E.g.

string from = "+1234567890", to = "+5678901234", message = "hi & bye+2";
//FormUrlEncodedContent results:
From=%2B1234567890&To=%2B5678901234&Body=hi+%26+bye%2B2
//StringContent results:
From=+1234567890&amp;To=+5678901234&amp;Body=hi & bye+2
Tim S.
  • 55,448
  • 7
  • 96
  • 122
  • @dotnet-practitioner From the [twilio documentation](http://www.twilio.com/docs/api/rest/request), Bad Request means "The data given in the POST or PUT failed validation. Inspect the response body for details." Look at that response body and let me know if you still have questions. And get rid of that `do..while` loop, that's not doing you any good: after you `await` or do `.Result`, the request is done, and nothing's going to change. – Tim S. Sep 23 '13 at 14:06
  • @dotnet-practitioner I've added a suggestion to use `FormUrlEncodedContent`. This makes a different request than what your `StringContent` code does. That might be the cause of your "Bad Request" error. – Tim S. Sep 23 '13 at 14:16
  • I was trying to get the example here https://www.twilio.com/blog/2011/09/using-the-twilio-rest-api-in-windows-8-metro-style-applications.html working without success. Changing to use FormUrlEncodedContent did the trick. – Philip Daniels Sep 04 '14 at 08:13