2016-02-19 56 views
0

這是從Dynamics CRM插件發出的GoCardless測試API請求。我收到「請求被中止:無法創建SSL/TLS安全通道。」錯誤。它只發生在一段時間後的第一次請求,而不發送一次。如果我再次發送它,那就OK了。我會很感激你的幫助。
這裏是我的代碼:C#「請求已中止:無法創建SSL/TLS安全通道。」 - 偶爾發生

//I have tried all the following lines in comment without success 
//ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate; 
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; 

//ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; 
//ServicePointManager.Expect100Continue = true; 
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; 

// Create a new WebClient instance. 
string baseURL = "https://api-sandbox.gocardless.com/"; 
WebClient client = new WebClient(); 
client.Headers.Add("Content-Type", "application/json"); 
client.Headers.Add("Authorization", "Bearer " + t); 
client.Headers.Add("GoCardless-Version", "2015-07-06"); 
client.Headers.Add("Accept", "application/json"); 
Customers model = new Customers();    
customer.country_code = "GB";    
model.customers = customer; 

MemoryStream stream1 = new MemoryStream(); 
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Customers)); 
ser.WriteObject(stream1, model); 
stream1.Position = 0; 
StreamReader sr = new StreamReader(stream1); 

// Apply ASCII Encoding to obtain the string as a byte array. 
byte[] byteArray = Encoding.ASCII.GetBytes(sr.ReadToEnd()); 

ReturnedCustomers result = new ReturnedCustomers(); 
//Upload the input string using the HTTP 1.0 POST method. 
try 
{ 
    byte[] responseArray = client.UploadData(baseURL + "customers", "POST", byteArray); 
    string responseText = Encoding.ASCII.GetString(responseArray); 
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ReturnedCustomers)); 

    using (Stream s = GenerateStreamFromString(responseText)) 
    { 
     result = (ReturnedCustomers)serializer.ReadObject(s); 
    } 
} 
catch (WebException exception) 
{ 

} 

回答

0

從微軟的文檔(https://msdn.microsoft.com/en-us/library/gg334752.aspx)以下限制:

  1. 只有HTTP和HTTPS協議是允許的。
  2. 不允許訪問本地主機(環回)。
  3. 無法使用IP地址。您必須使用需要DNS名稱解析的指定網址。
  4. 支持並推薦匿名身份驗證。 5.沒有規定提示登錄用戶輸入憑證或保存這些憑證。

該錯誤可能是由於seguneti事情:

  1. 的證書無效
  2. 的認證機構是不公開
  3. 你能檢查什麼是ServicePointManager.Expect100Continue和ServicePointManager的價值.SecurityProtocol屬性在您的環境中?
相關問題