0

过去几周我一直在使用 clickatell 服务,每天早上向多个手机号码发送短信,直到今天没有问题:

System.Net.WebException:基础连接已关闭:发送时发生意外错误。---> System.IO.IOException: 身份验证失败,因为远程方已关闭传输流。

我尝试过从公司网络内部发送,也尝试过从公司网络外部发送,甚至尝试通过移动数据连接,每次都得到相同的响应。

我检查了我的 clickatell 帐户,它仍然有足够的信用,并且集成已“打开”..

任何想法出了什么问题?

4

2 回答 2

0

在clickatell没有回应之后,我做了一些挖掘,发现了这个线程......

“底层连接已关闭:发送时发生意外错误。” 使用 SSL 证书

当我使用.net 4时,我尝试将这行代码添加到我从clickatell获得的Rest Class中......

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

添加此代码似乎使系统再次工作..

因此,如果您使用的是 .net4 和 REST api,这就是您需要的代码:

class Rest
{
    //This takes the API Key and JSON array of data and posts it to the Message URL to send the SMS's
    public static string Post(string Token, string json)
    {
        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://platform.clickatell.com/messages");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";
        httpWebRequest.Accept = "application/json";
        httpWebRequest.PreAuthenticate = true;
        httpWebRequest.Headers.Add("Authorization", Token);

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
        }
        ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
            return result;
        }
   }
}

希望这对其他人有帮助....

于 2017-07-26T08:39:39.943 回答
0

是的,SecurityProtocol 必须是 Transport Layer Security 1.2(3072 它是 Tls12 SecurityProtocolType 枚举值)。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

在 .net 4.6 中,Microsoft 升级了对 ssl 的安全限制。

如果您想了解更多,请阅读:ServicePointManager o SslStream APIs in .net 4.6

抱歉,这篇文章是意大利语的,我没有找到英文版本。

于 2017-07-29T04:27:27.827 回答