http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 7900|回复: 3

[咨询站长] 无法建立ssl连接,身份验证失败,因为远程方已关闭传输流。

[复制链接]
发表于 2020-10-9 16:26:53 | 显示全部楼层 |阅读模式
多次请求微信的接口(检查文本合法性)时会出现错误:
the ssl connection could not be established,authentication failed because the remote party has closed the transport stream.


[C#] 纯文本查看 复制代码
public static async Task<string> PostAsync(string url, HttpContent content, Dictionary<string, string> headers) {
            try {
                var handler = new HttpClientHandler() {
                    ServerCertificateCustomValidationCallback = delegate { return true; },
                    ClientCertificateOptions = ClientCertificateOption.Automatic,
                    SslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12
                };

                using (HttpClient client = new HttpClient(handler)) {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13;
                    if (headers != null) {
                        if (content != null) {
                            foreach (var headerItem in headers) {
                                content.Headers.Add(headerItem.Key, headerItem.Value);
                            }
                        } else {
                            foreach (var headerItem in headers) {
                                client.DefaultRequestHeaders.Add(headerItem.Key, headerItem.Value);
                            }
                        }
                    }

                    var response = await client.PostAsync(url, content);

                    var result = await response.Content.ReadAsStringAsync();

                    return result;
                }
            } catch (Exception ex) {
                throw ex;
            }
        }



补充内容 (2020-10-9 17:04):
2020-09-24 18:49:38,465 ERROR InfoLogger - One or more errors occurred. (The SSL connection could not be established, see inner exception.)
System.AggregateException: One or more errors occurred. (...


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
 楼主| 发表于 2020-10-9 17:05:42 | 显示全部楼层

补充异常信息

[C#] 纯文本查看 复制代码
2020-09-24 18:49:38,465 ERROR InfoLogger - One or more errors occurred. (The SSL connection could not be established, see inner exception.)
System.AggregateException: One or more errors occurred. (The SSL connection could not be established, see inner exception.)
 ---> System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
 ---> System.IO.IOException: Authentication failed because the remote party has closed the transport stream.
   at System.Net.Security.SslStream.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslStream.PartialFrameCallback(AsyncProtocolRequest asyncRequest)
--- End of stack trace from previous location where exception was thrown ---
   at System.Net.Security.SslStream.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Net.Security.SslStream.EndProcessAuthentication(IAsyncResult result)
   at System.Net.Security.SslStream.EndAuthenticateAsClient(IAsyncResult asyncResult)
   at System.Net.Security.SslStream.<>c.<AuthenticateAsClientAsync>b__65_1(IAsyncResult iar)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
发表于 2020-10-10 09:43:08 | 显示全部楼层
用我的Httphelper类看看提示什么
 楼主| 发表于 2020-10-10 11:45:49 | 显示全部楼层
站长苏飞 发表于 2020-10-10 09:43
用我的Httphelper类看看提示什么





这个是用HttpHelper后测试500次过程中出现的错误
[C#] 纯文本查看 复制代码
HttpHelper http = new HttpHelper();
                HttpItem item = new HttpItem() {
                    URL = url,
                    Postdata = JsonConvert.SerializeObject(request),
                    PostDataType = PostDataType.String,
                    SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls
                };

                HttpResult response = http.GetHtml(item);

                if(response.StatusCode == HttpStatusCode.OK) {
                    return ConvertResult(response.Html);
                }
                LogHelper.InfoFormat("返回错误{0}", response.Html);
                return false;



您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-11-24 15:05

© 2014-2021

快速回复 返回顶部 返回列表