C# clickatell发送短信HTTP GET请求

C# clickatell to send SMS HTTP GET request

我想使用服务 Clickatell 发送一条简单的消息。

我不想读取响应,发送消息将是简单的 GET 请求。

该服务提供的请求如下所示:

https://platform.clickatell.com/messages/http/send?apiKey=xxxxxxxxxxxxxxxx==&to=xxxxxxxxxxx&content=Test+message+text

我用curl查了一下

curl "https://platform.clickatell.com/messages/http/send?apiKey=apiKEY==&to=NUMBER&content=Test+message+text"

它工作得很好。

我尝试将它与带有 HTTP 请求的 Windows Forms 应用程序一起使用 以下是我提供的代码:

var client2 = new HttpClient();
client2.GetAsync("https://platform.clickatell.com/messages/http/send?apiKey=apiKEY==&to=NUMBER&content=Test+message+text");
App.Write("SMS SEND!");

我有短信发送的信息,但我没有收到。我的朋友在 .NET 应用程序中使用我的代码,它对他有用。

我错过了什么吗?

也许值得一提的是我需要使用 System.Net.Http;

手动添加到引用

编辑:

我尝试添加异步操作,因此我将代码编辑为:

   static void sendSMS()
        {
            var client2 = new HttpClient();
            var task = client2.GetAsync("https://platform.clickatell.com/messages/http/send?apiKey=API_KEY==&to=MY_NUMBER&content=Test+message+text");
            task.Wait();
            App.Write("SMS SEND!");
        }

但是现在应用程序中的SMS SEND消息没有显示。

好的,我知道您使用的是 .NET 4.5,并且您可能遇到异常问题

"The underlying connection was closed: An unexpected error occurred on a send"

正确的代码是这样的:(你必须在reqeust之前添加'SecurityProtocol'):

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

var client2 = new HttpClient(); client2.GetAsync("https://platform.clickatell.com/messages/http/send?apiKey=apiKEY==&to=NUMBER&content=Test+message+text").Result;

此处有更多详细信息