微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Recaptcha PostAsync 抛出 System.Net.WebException:底层连接已关闭

如何解决Recaptcha PostAsync 抛出 System.Net.WebException:底层连接已关闭

尝试实施谷歌 Recaptcha,在本地运行良好。但是当部署到其他服务器(通过八达通)时,它会抛出异常。无法找到原因。发生在 PostAsync 调用上。请需要帮助。

  using (HttpClient client = new HttpClient())

{
                System.Net.ServicePointManager.SecurityProtocol = System.Net.ServicePointManager.SecurityProtocol | System.Net.SecurityProtocolType.Tls12;
                client.Timeout = TimeSpan.FromMilliseconds(this.ValidationTimeoutMiliseconds);                    



                var values = new Dictionary<string,string>
                {
                   { "secret",Secret },{ "response",value as string }
                };



                LogHelper.WriteLog("Start: Recaptcha requestdata is about to post to google...");
                var response = client.PostAsync("https://www.google.com/recaptcha/api/siteverify",new FormUrlEncodedContent(values)).GetAwaiter().GetResult();
                LogHelper.WriteLog("End: Recaptcha requestdata is posted successfully");



                var responseString = response.Content.ReadAsstringAsync().Result;
                var responseModel = JsonConvert.DeserializeObject<GRecaptchaValidationResponseModel>(responseString);



                if (responseModel.ErrorCodes != null && responseModel.ErrorCodes.Contains("invalid-input-secret"))
                    LogHelper.WriteLogAsync("Invalid input secret for reCaptcha. Please configure in advanced settings parameters for Forms.");



                return responseModel.Success;
 }

抛出异常: 发布到 google recaptcha 时发生异常:System.Net.WebException:底层连接已关闭:发送时发生意外错误。 ---> System.IO.IOException: 无法从传输连接读取数据:远程主机强行关闭了现有连接。 ---> System.Net.sockets.socketException: 一个现有的连接被远程主机强行关闭 在 System.Net.sockets.socket.EndReceive(IAsyncResult asyncResult) 在 System.Net.sockets.NetworkStream.EndRead(IAsyncResult asyncResult) --- 内部异常堆栈跟踪结束 --- 在 System.Net.Tlsstream.EndWrite(IAsyncResult asyncResult) 在 System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar) --- 内部异常堆栈跟踪结束 --- 在 System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult,TransportContext& context) 在 System.Net.Http.httpclienthandler.GetRequestStreamCallback(IAsyncResult ar)

解决方法

谢谢 已解决,服务器中存在防火墙问题阻止。解决后,工作正常。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?