2017-07-25 157 views
0

我一直在使用的Clickate服務過去幾周每天早晨發送短信到多個手機號碼的,直到今天沒有任何問題:的Clickate未能發送短信 - C#WinForms應用程序

System.Net.WebException:基礎連接已關閉:發送時發生意外錯誤。 ---> System.IO.IOException:由於遠程方關閉了傳輸流,認證失敗。

我試過從企業網絡內部發送,並且在外面也嘗試了移動數據連接,每次都得到相同的響應。

我檢查了我的Clickate帳戶,它仍然有大量的信貸,並整合爲「ON」切換..

任何想法是什麼問題呢?

回答

0

缺乏從響應的Clickate後,我做了一些挖了一圈,發現這個線程...

"The underlying connection was closed: An unexpected error occurred on a send." With SSL Certificate

由於我使用的是.NET 4,我嘗試添加這行代碼到其餘的I類從的Clickate了......

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; 
     } 
    } 
} 

希望這可以幫助別人....

0

是的,SecurityProtocol必須是傳輸層安全1.2(3072是Tls12 SecurityProtocolType枚舉值)。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 

With .net 4.6 Microsoft已經升級了對ssl的安全限制。

如果您想了解更多,請閱讀本:ServicePointManager o SslStream APIs in .net 4.6

對不起,這篇文章是在意大利,我沒有找到英文版本。