You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调试SharePoint API时出现“The SSL connection could not be established”错误求助

SharePoint CSOM/PnP.Framework 执行ExecuteQuery()时出现SSL连接被强制关闭错误

我在沙箱SharePoint站点开发文件管理工具,使用PnP.Framework和Microsoft.SharePointOnline.CSOM搭建控制台应用。前10分钟调试正常,之后执行ExecuteQuery()时抛出SSL连接错误,错误栈显示远程主机强制关闭了连接。

代码示例:

string clientId = "*********";
string redirectUrl = "http://localhost";
string tenantId = "************";
string siteUrl = "https://5klmt2.sharepoint.com/sites/TestTeam";

var authMan = PnP.Framework.AuthenticationManager.CreateWithInteractiveLogin(clientId, redirectUrl, tenantId);

using (ClientContext context = authMan.GetContext(siteUrl))
{
    var web = context.Web;
    context.Load(web, w => w.Id, w => w.Title);
    context.ExecuteQuery();

    Console.WriteLine($"{web.Id} - {web.Title}");

    var documents = web.GetListByTitle("Documents", l => l.Id, l => l.Title);

    Console.WriteLine($"{documents.Id} - {documents.Title}");
}

错误信息:

System.Net.WebException
  HResult=0x80131620
  Message=The SSL connection could not be established, see inner exception.
  Source=System.Net.Requests
  StackTrace:
   at System.Net.HttpWebRequest.GetResponse()
   at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute()
   at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)
   at Microsoft.SharePoint.Client.ClientRequest.ExecuteQuery()
   at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQuery()
   at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
   at Program.<TestStuff>d__1.MoveNext() in C:\Club Benchmarking\GitHub\SharePointCSOM\SharePointCSOM\Program.cs:line 29
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_1(Object state)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()

Inner Exception 1:
HttpRequestException: The SSL connection could not be established, see inner exception.

Inner Exception 2:
IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host..

Inner Exception 3:
SocketException: An existing connection was forcibly closed by the remote host.

解决办法

  • 更新依赖包到最新版本
    旧版本的PnP.Framework和CSOM库可能存在TLS握手、连接复用的已知bug,通过NuGet将所有相关包更新到最新稳定版,同步修复依赖组件的SSL相关问题。

  • 强制指定TLS版本
    SharePoint Online要求使用现代TLS协议,在代码最开头添加强制声明,避免系统默认使用旧版本协议导致连接被拒:

    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls13;
    
  • 复用AuthenticationManager实例
    反复创建认证管理器可能导致连接池耗尽或会话冲突,尝试全局复用一个AuthenticationManager实例,而非每次请求都新建。

  • 检查网络与代理配置
    企业网络下的代理服务器可能中断长连接,可切换到非企业网络测试;若必须使用代理,给ClientContext配置代理信息:

    context.Proxy = new WebProxy("http://your-proxy-address:port", true);
    
  • 添加超时与重试机制
    临时网络波动可能导致连接断开,手动添加重试逻辑或调整请求超时时间:

    using (ClientContext context = authMan.GetContext(siteUrl))
    {
        context.RequestTimeout = 30000; // 设置30秒超时
        bool success = false;
        int retryCount = 0;
        while (!success && retryCount < 3)
        {
            try
            {
                var web = context.Web;
                context.Load(web, w => w.Id, w => w.Title);
                context.ExecuteQuery();
                success = true;
            }
            catch (WebException)
            {
                retryCount++;
                Thread.Sleep(1000 * retryCount);
            }
        }
    }
    
  • 验证应用权限与会话有效性
    确认Azure AD中应用注册的权限(如Sites.Read.All)已被授予,且交互式登录的会话未过期,避免因权限或会话问题导致连接被强制关闭。


内容的提问来源于stack exchange,提问作者Brandon Armstrong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 22:33:35