我将xml数据发送到UPS网站,有时我得到回复,有时我得到异常:基础连接已关闭
查看将xml发送到特定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 选项吗?
所以请有人开车帮助我摆脱这个错误。
告诉我发生错误的所有可能原因。感谢
答案 0 :(得分:3)
实际上这些代码解决了我的问题
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;
}
}