C#"请求已中止:无法创建SSL / TLS安全通道。" - 偶尔发生

时间:2016-02-19 10:00:19

标签: ssl dynamics-crm webclient

这是对来自Dynamics CRM插件的GoCardless测试API的请求。我收到"请求已中止:无法创建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)
{

}

1 个答案:

答案 0 :(得分:0)

从Microsoft文档(https://msdn.microsoft.com/en-us/library/gg334752.aspx)有以下限制:

  1. 仅允许使用HTTP和HTTPS协议。
  2. 不允许访问localhost(环回)。
  3. 无法使用IP地址。您必须使用需要DNS名称解析的命名Web地址。
  4. 支持并建议使用匿名身份验证。 5.没有规定提示登录用户提供凭据或保存这些凭据。
  5. 错误可能是因为seguneti的事情:

    1. 证书无效
    2. 证书颁发机构不公开
    3. 您可以查看环境中ServicePointManager.Expect100Continue和ServicePointManager.SecurityProtocol属性的值是什么?