使用ASP.NET和Twilio API验证电话号码

时间:2012-07-23 07:32:42

标签: asp.net twilio

我正在尝试使用以下代码检查数字是否有效。这有用吗?

try
    {
        TwilioRest.Account account = new TwilioRest.Account(TwilioRest.TwilioConstants.ACCOUNT_SID, TwilioRest.TwilioConstants.ACCOUNT_TOKEN);
        string strResponse = account.request(String.Format("/{0}/Accounts/{1}/OutgoingCallerIds", TwilioRest.TwilioConstants.API_VERSION, TwilioRest.TwilioConstants.ACCOUNT_SID), "GET");
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(strResponse);
        XmlNode root = xmlDoc.DocumentElement.ChildNodes[0];
        int cont = 0;
        if (root.ChildNodes.Count > 0)
        {
            for (int i = 0; i < root.ChildNodes.Count; i++)
            {
                string ss = root.ChildNodes[i].SelectSingleNode("PhoneNumber").ChildNodes[0].Value;
                if (phoneno.Trim() == ss)
                {
                    cont = 1;
                    break;
                }
            }
        }
        if (cont == 0)
        {
            btnSubmit.Visible = true;
        }
    }
    catch (TwilioRest.TwilioRestException ex)
    {
        mpValidatePhone.Show();
        ucMsgPhone.MessageType = MessageType.Error;
        ucMsgPhone.Text = TwilioErrorMessage(ex.ToString());
    }

1 个答案:

答案 0 :(得分:1)

尝试这样....

string strResponse = account.request(String.Format("/{0}/Accounts/{1}/OutgoingCallerIds",
            TwilioRest.TiwlioConstants.API_VERSION, TwilioRest.TiwlioConstants.ACCOUNT_SID), "GET");
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(strResponse);
        XmlNode root = xmlDoc.DocumentElement.ChildNodes[0];
        foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes[0])
        {
            string nodeVal = node.SelectSingleNode("PhoneNumber").ChildNodes[0].Value.ToString();
            string fonVal = foneCountryCode + PhoneNumber.Replace("-", "");
            //if (node.SelectSingleNode("PhoneNumber").ChildNodes[0].Value == "+" + PhoneNumber.Replace("-", ""))
            if(nodeVal == fonVal)
            {
                result = true;
                break;
            }
        }