C# BigCommerce API连接问题/开始时是间歇性的,现在更为频繁
我已经使用API将订单推送到我们客户的BigCommerce商店很长一段时间了,但是,BigCommerce最近开始拒绝连接和/或关闭连接 我一直无法找到问题的根源,我希望有人有经验和/或可以帮助找到问题的根源 以下是我们现在对所有大型商业API请求的响应: 消息:基础连接已关闭:发送时发生意外错误 InnerException:System.IO.IOException:身份验证失败,因为远程方已关闭传输流。在 System.Net.Security.SslState.StartReadFrame(字节[]缓冲区,Int32 readBytes,AsyncProtocolRequest(异步请求)位于 System.Net.Security.SslState.StartReceiveBlob(字节[]缓冲区, AsyncProtocolRequest(异步请求)位于 System.Net.Security.SslState.StartSendBlob(字节[]传入,Int32 计数,异步协议请求(异步请求) System.Net.Security.SslState.ForceAuthentication(布尔值receiveFirst, 字节[]缓冲区,异步协议请求(异步请求) System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult 懒散的(结果) System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext、ContextCallback回调、对象状态、布尔值 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback回调、对象状态、布尔值 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback回调、对象状态)位于 System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult结果)位于 System.Net.TlsStream.Write(字节[]缓冲区,Int32偏移量,Int32大小)位于 System.Net.ConnectStream.WriteHeaders(布尔异步)C# BigCommerce API连接问题/开始时是间歇性的,现在更为频繁,c#,.net,bigcommerce,C#,.net,Bigcommerce,我已经使用API将订单推送到我们客户的BigCommerce商店很长一段时间了,但是,BigCommerce最近开始拒绝连接和/或关闭连接 我一直无法找到问题的根源,我希望有人有经验和/或可以帮助找到问题的根源 以下是我们现在对所有大型商业API请求的响应: 消息:基础连接已关闭:发送时发生意外错误 InnerException:System.IO.IOException:身份验证失败,因为远程方已关闭传输流。在 System.Net.Security.SslState.StartReadFra
现在每次都是这样吗?如果没有,频率是多少?当您说“拒绝连接和/或关闭连接”时,您是否看到针对每种情况的两种不同错误响应 我以前从BC看到过类似的消息,但只是来自格式错误的请求,这听起来不像您的情况,因为代码以前工作正常。回家后,我将运行一些测试,看看是否看到类似的问题,并比较代码,看看是否存在差异 编辑:发布我正在使用的代码的一个非常简化的版本可能会更有帮助。我有一个辅助方法BigCommerceGet,它在代码中的多个位置调用:
private string BigCommerceGet(string URL)
{
System.Net.HttpWebRequest req = (HttpWebRequest)WebRequest.Create(baseUrl + URL);
req.Credentials = new NetworkCredential(_username, _api_key);
req.AllowAutoRedirect = true;
req.ContentType = "application/json";
req.Accept = "application/json";
req.Method = "GET";
string jsonResponse = null;
using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
{
if (req.HaveResponse && resp != null)
{
using (var reader = new StreamReader(resp.GetResponseStream()))
{
jsonResponse = reader.ReadToEnd();
}
}
}
return jsonResponse;
}
以下是我用来从我的站点检索所有订单并将其写入文件的循环:
public Order[] GetAllOrders()
{
Order[] result = null;
string orderString = "";
try
{
StringBuilder orders = new StringBuilder("[");
String jsonResponse = BigCommerceGet("orders?limit=50&page=1");
int page = 1;
string prePend = "";
while (jsonResponse != "")
{
// Remove the leading and trailing brackets, and prepend a comma
// beyond page 1.
orders.Append(prePend + jsonResponse.Substring(1, jsonResponse.Length - 2));
prePend = ",";
page++;
jsonResponse = BigCommerceGet("orders?limit=50&page=" + page.ToString());
}
orders.Append("]");
System.IO.FileStream wFile;
byte[] byteData = null;
byteData = Encoding.ASCII.GetBytes(orders.ToString());
using (wFile = new FileStream(@"Z:\ThisIsYourFile.txt", FileMode.Create))
{
wFile.Write(byteData, 0, byteData.Length);
wFile.Close();
}
orderString = orders.ToString();
result = JsonConvert.DeserializeObject<Order[]>(orderString);
}
catch (Exception e)
{
Console.WriteLine("*** Exception encountered while retrieving store information: {0}", e.ToString());
}
return result;
}
公共秩序[]GetAllOrders()
{
订单[]结果=null;
字符串orderString=“”;
尝试
{
StringBuilder订单=新StringBuilder(“[”);
字符串jsonResponse=bigcmerceget(“订单?限制=50&page=1”);
int page=1;
字符串prePend=“”;
while(jsonResponse!=“”)
{
//删除前括号和后括号,并在前加逗号
//超出第1页。
Append(prePend+jsonResponse.Substring(1,jsonResponse.Length-2));
prePend=“,”;
page++;
jsonResponse=BigCommerceGet(“订单?限额=50&page=“+page.ToString());
}
订单。追加(“]”);
System.IO.FileStream wFile;
字节[]字节数据=null;
byteData=Encoding.ASCII.GetBytes(orders.ToString());
使用(wFile=newfilestream(@“Z:\ThisIsYourFile.txt”,FileMode.Create))
{
Write(byteData,0,byteData.Length);
wFile.Close();
}
orderString=orders.ToString();
结果=JsonConvert.DeserializeObject(orderString);
}
捕获(例外e)
{
WriteLine(“***检索存储信息时遇到异常:{0}”,e.ToString());
}
返回结果;
}
您应该能够对此进行修改,以验证是否可以从您的站点一致地检索订单。与BC通信后,BC API服务器上似乎禁用了TLS 1.0,从而导致来自运行IIS的Windows 2008 R2服务器的请求出现问题 SSL 3.0以前已被BC禁用,BC不会在IIS上为我抛出错误,因为SSL 3.0也已在我的服务器上禁用 对于遇到类似问题的用户,建议禁用SSL和TLS 1.0(BC将在不久的将来弃用TLS 1.0协议),只保留较新的协议 不列颠哥伦比亚省的进一步说明: *只是想告诉你-我们能够在Windows Server 2008计算机上重现同样的问题。它看起来确实像TLS/SSL协商,特别是因为2k8只支持SSLv3(长时间禁用)和TLS1.0。我们禁用了TLS 1.0[删除],这是我们迁移到新的负载平衡器的一部分,我们的理解是,我们应该担心Windows Vista及以下版本。不幸的是,2k8共享相同的密码配置 [删除] 在接下来的一个月左右的时间里,我将和我们的团队一起积极地反对TLSv1.0和不安全的密码用于API流量。这是我们今天的一小部分流量。我们将对此进行适当的沟通,但这将迫使您转向更新的操作系统。
*大约一周前,它开始偶尔发生,但每次请求都会持续发生。来自BC的错误响应总是相同的。我们这边没有任何变化,我们已经使用相同的代码一年多了,没有任何问题。似乎BC现在可能需要某种类型的安全连接?感谢您提供的额外见解,但事实证明这是一个TLS/SSL协商问题。下面答案中的注释似乎与SSL有关。您可以忽略证书验证吗?或显示ssl
public Order[] GetAllOrders()
{
Order[] result = null;
string orderString = "";
try
{
StringBuilder orders = new StringBuilder("[");
String jsonResponse = BigCommerceGet("orders?limit=50&page=1");
int page = 1;
string prePend = "";
while (jsonResponse != "")
{
// Remove the leading and trailing brackets, and prepend a comma
// beyond page 1.
orders.Append(prePend + jsonResponse.Substring(1, jsonResponse.Length - 2));
prePend = ",";
page++;
jsonResponse = BigCommerceGet("orders?limit=50&page=" + page.ToString());
}
orders.Append("]");
System.IO.FileStream wFile;
byte[] byteData = null;
byteData = Encoding.ASCII.GetBytes(orders.ToString());
using (wFile = new FileStream(@"Z:\ThisIsYourFile.txt", FileMode.Create))
{
wFile.Write(byteData, 0, byteData.Length);
wFile.Close();
}
orderString = orders.ToString();
result = JsonConvert.DeserializeObject<Order[]>(orderString);
}
catch (Exception e)
{
Console.WriteLine("*** Exception encountered while retrieving store information: {0}", e.ToString());
}
return result;
}