【问题标题】:Twilio XML response to SMSTwilio XML 对 SMS 的响应
【发布时间】:2016-06-24 21:25:53
【问题描述】:

我正在尝试使用 Twilio 完成一个示例 SMS 应用程序,在该应用程序中我向我的 Twilio 号码发送一条 SMS 消息,然后 Twilio 服务将回复给我。我知道 Twilio 服务正在到达我的 API,因为我可以看到来自 Twilio 的传入请求到达我的 API,并且我可以看到我的 API 的响应,但我认为有些地方是错误的,因为我从来没有收到短信响应。

[HttpPost]
[Route("EchoTest")]
public IHttpActionResult EchoTest()
{
    string response = "<Response><Sms>Thanks for the message</Sms></Response>";
    return ResponseMessage(Request.CreateResponse(HttpStatusCode.OK, response, new XmlMediaTypeFormatter()));
}

我将返回ResponseMessage,因此我可以始终如一地返回IHttpActionResult。我也尝试只返回HttpResponseMessage,如下所示,结果相同。

[HttpPost]
[Route("EchoTest")]
public HttpResponseMessage EchoTest()
{
    string response = "<Response><Sms>Thanks for the message</Sms></Response>";
    Request.CreateResponse(HttpStatusCode.OK, response, new XmlMediaTypeFormatter());
}

这是我的 API 发回的内容...

<string
 xmlns="http://schemas.microsoft.com/2003/10/Serialization/">&lt;Response&gt;&lt;Sms&gt;Thanks for the message&lt;/Sms&gt;&lt;/Response&gt;
</string>

我是否弄乱了 XML 响应?我希望发回给 Twilio 的是这个......

<Response><Sms>Thanks for the message</Sms></Response>

【问题讨论】:

    标签: c# xml twilio twilio-api


    【解决方案1】:

    查看网页:https://msdn.microsoft.com/en-us/library/hh969056(v=vs.118).aspx 试试这个

    XElement response = XElement.Parse("<Response><Sms>Thanks for the message</Sms></Response>");
        Request.CreateResponse<XElement>(request, HttpStatusCode.OK, response);
    

    【讨论】:

    • 谢谢! XElement 代码有效。尽管我确实必须使用return Request.CreateResponse&lt;XElement&gt;(HttpStatusCode.OK, response, new XmlMediaTypeFormatter()); 将其发回
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 2022-10-12
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多