Исключение C# WebClient: базовое соединение было закрыто

я отправляю XML-данные на веб-сайт UPS, иногда получаю ответ, а иногда получаю Исключение: базовое соединение было закрыто

см. пример кода, который отправляет xml на определенный URL-адрес UPS.

    public ShippingAcceptResult ShippingAccept(string acceptDigestCode)
    {
    string strXml = "";
    try
    {
        xmlRequest = new System.Xml.XmlDocument();
        xmlRequest.LoadXml(@"<?xml version=""1.0""?>
     <ShipmentAcceptRequest>
       <Request>
          <TransactionReference>
         <CustomerContext>TR01</CustomerContext>
         <XpciVersion>1.0001</XpciVersion>
          </TransactionReference>
          <RequestAction>ShipAccept</RequestAction>
          <RequestOption>01</RequestOption>
       </Request>
       <ShipmentDigest>" + acceptDigestCode + "</ShipmentDigest></ShipmentAcceptRequest>");

        byte[] bb = new System.Text.ASCIIEncoding().GetBytes(string.Format(XML_CONNECT, sAccessCode.Trim(), sUserId.Trim(), sPassword.Trim()));
        byte[] bResponse = null; 
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        ms.Write(bb, 0, bb.Length);
        xmlRequest.Save(ms);
        bb = ms.ToArray();
        xmlRespone = new XmlDocument();
        string serverName = (testMode) ? "wwwcie" : "onlinetools.ups.com";
        serverName = string.Format(UPS_SERVICE_URL, serverName, ShipRequestTypes.ShipAccept);

        using (var client = new NoKeepAlivesWebClient())
        {
        bResponse = client.UploadData(serverName, "POST", bb);
        }

        if (bResponse != null)
        {
        xmlRespone.LoadXml(System.Text.ASCIIEncoding.ASCII.GetString(bResponse));
        }
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show("ShippingAccept " + ex.Message);
        System.Windows.Forms.MessageBox.Show(strXml);
    }
    return new ShippingAcceptResult(xmlRespone);

}

public class NoKeepAlivesWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
        ((HttpWebRequest)request).KeepAlive = false;
        }

        return request;
    }
}

раньше я не делал set KeepAlive = false, тогда я также получал такое же сообщение об ошибке, но теперь, когда я установил KeepAlive = false, то также некоторое время получал ту же ошибку, связанную с закрытием соединения.

подскажите ошибка специфична для любого пк?

Должен ли я установить значение тайм-аута для webclient, например для параметра KeepAlive?

поэтому, пожалуйста, направьте меня в правильном направлении, чтобы избавиться от этой ошибки.

скажите мне все возможные причины, по которым происходит ошибка. Благодарность


person Thomas    schedule 02.11.2017    source источник
comment
возможно, трассировка стека поможет найти ответ.   -  person LenglBoy    schedule 02.11.2017
comment
если это https, убедитесь, что вы принимаете сертификат (отредактируйте), не ждите, игнорируйте это, если иногда это работает, но не другие, на тот же URL-адрес   -  person mikelegg    schedule 02.11.2017


Ответы (1)


на самом деле эта строка кода решает мою проблему

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; //768 for TLS 1.1 and 3072 for TLS 1.2
//ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate;
//System.Net.ServicePointManager.ServerCertificateValidationCallback += (send, certificate, chain, sslPolicyErrors) => { return true; };
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

еще несколько фрагментов кода, которые я использовал вместе с ServicePointManager

using (var client = new NoKeepAlivesWebClient())
{
    bResponse = client.UploadData(serverName, "POST", bb);
}

/// Certificate validation callback.
/// </summary>
private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
{
    // If the certificate is a valid, signed certificate, return true.
    if (error == System.Net.Security.SslPolicyErrors.None)
    {
    return true;
    }

    Console.WriteLine("X509Certificate [{0}] Policy Error: '{1}'",
    cert.Subject,
    error.ToString());

    return false;
}

public class NoKeepAlivesWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
        ((HttpWebRequest)request).KeepAlive = false;
        ((HttpWebRequest)request).Timeout = 60000;
        }

        return request;
    }
}
person Thomas    schedule 13.11.2017
comment
Похоже, ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072; исправил мою проблему с реализацией, которая работала очень долго и внезапно остановилась несколько дней назад. Возможно после январского обновления. - person Lucas van Dongen; 23.01.2018
comment
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; была линия, которую я имел в виду - person Lucas van Dongen; 23.01.2018