Error : The status callback on localhost is not a valid URL

Error : The status callback on localhost is not a valid URL

我正在尝试制作一个可以使用 twilio 发送短信的应用程序 API

这个 API 包含 statusCallback 属性,我们可以包含一个 link,API 可以发送有关传递行为的数据(如果收到短信等...)

public void sendSMS()
     foreach (var toNumber in TOnumbersList)
            {
             var message = MessageResource.Create(
             to: new PhoneNumber(toNumber),
             from: new PhoneNumber(fromNumber),
             body: msgBody,
             provideFeedback: true,
             statusCallback: new Uri("http://localhost:5000/"));// <---- 

              }

正如您所注意到的,在 statusCallback 中我明确表示我想在 localhost:5000/

上发送信息

在我的 visual studio 2015 解决方案资源管理器中,我添加了一个单独的项目并将其命名为 windows 服务

  using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.ServiceProcess;
using System.Text;

    using System.Threading;
    using System.Threading.Tasks;

    namespace WindowsService
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            static void Main()
            {

                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[]
                {
                    new Service1()

                };
             //   ServiceBase.Run(ServicesToRun);

                _httpListener.Prefixes.Add("http://localhost:5000/"); // add prefix "http://localhost:5000/"
                _httpListener.Start(); // start server (Run application as Administrator!)
                Console.WriteLine("Server started.");
                Thread _responseThread = new Thread(ResponseThread);
                _responseThread.Start(); // start the response thread

            }
            static HttpListener _httpListener = new HttpListener();
            static void ResponseThread()
            {
                while (true)
                {
                    HttpListenerContext context = _httpListener.GetContext(); // get a context
                   var a = context.Request.Url;                                        // Now, you'll find the request URL in context.Request.Url
                    byte[] _responseArray = Encoding.UTF8.GetBytes("<html><head><title>Localhost server -- port 5000</title></head>" +
                    "<body>Welcome to the <strong>Localhost server</strong> -- <em>port 5000!</em></body></html>"); // get the bytes to response
                    context.Response.OutputStream.Write(_responseArray, 0, _responseArray.Length); // write bytes to the output stream
                    context.Response.KeepAlive = false; // set the KeepAlive bool to false
                    context.Response.Close(); // close the connection
                    Console.WriteLine("Respone given to a request.");
                }
            }
        }
    }

之后,在解决方案的配置中,我明确指出我希望在 windows 表单项目之前为 运行 提供服务,您可以在图片中注意到

因此,一旦我开始解决方案,我就继续 localhost:5000 并注意到服务 运行ning 并且页面显示欢迎消息

但是,一旦调用(或调用)sendSMS() 函数(当服务处于 运行ning 时)我收到此错误:

Error : The status callback on localhost is not a valid URL

所以我的问题是我做错了什么?我想知道我对网络服务技术不是很熟悉,我是不是忘了启用某些东西?或者与异步和同步问题相关的东西?

注意:在过去,(而不是使用 local-host ),我使用 http://requestb.in/ 创建了一个 url (它完成了网络服务的工作)并且我复制了创建的url 并且在 link 上发送数据没有问题。但是当 link 是 localhost 时,会注意到问题

"Localhost" 解析到代码当前 运行 所在的机器,即 127.0.0.1 (IPv4) 和 ::1 (IPv6)。

如果您将 localhost 传递给 Twilio 等远程服务,远程服务会将 "localhost" 解析为 127.0.0.1/::1,即它本身。这没有任何意义,这就是 Twilio 拒绝 URL.

的原因

您指定的回调 URL 必须 public 并且可以从 Twilios 服务访问,否则这将不起作用。我通常使用便宜或免费的 Azure 网站进行此类测试。