【问题标题】:ReCAPTCHA fails a lot with "An existing connection was forcibly closed by the remote host" errorReCAPTCHA 因“现有连接被远程主机强行关闭”错误而失败很多
【发布时间】:2014-01-02 19:30:33
【问题描述】:

我在 Azure 云中托管的 MVC4 应用程序中使用 ReCAPTCHA 来创建一个带有一个注册表单的简单网站。目前,我们每小时大约有 100-120 次成功注册。问题是我在日志中有数百个System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host 错误,然后数字一直在快速增长:

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> 
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> 
System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> 
System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)     
--- End of inner exception stack trace ---     
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)     
at System.Net.Connection.ReadCallback(IAsyncResult asyncResult)     
--- End of inner exception stack trace ---     
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)     
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)     
--- End of inner exception stack trace ---     
--- End of inner exception stack trace ---     
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)     
at My.Web.Infrastructure.Filters.ValidateReCaptchaAttribute.OnActionExecuting(ActionExecutingContext filterContext) in My.Web\Infrastructure\Filters\ValidateReCaptchaAttribute.cs:line 49  ---> 
(Inner Exception #0) System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> 
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> 
System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> 
System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host     
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)     
--- End of inner exception stack trace ---

我使用属性来验证验证码如下(我删除了一些不重要的细节):

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class ValidateReCaptchaAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var formValues = new[]
        {
            new KeyValuePair<string, string>("privatekey", ConfigurationProvider.ReCaptchaPrivateKey), 
            new KeyValuePair<string, string>("remoteip", remoteIp.ToString()),
            new KeyValuePair<string, string>("challenge", challengeField), 
            new KeyValuePair<string, string>("response", responseField)
        };

        try
        {
            using (var client = HttpClientFactory.Create())
            using (var data = new FormUrlEncodedContent(formValues))
            using (var response = client.PostAsync("http://www.google.com/recaptcha/api/verify", data).Result)
            {
                var responseString = response.Content.ReadAsStringAsync().Result;
                if (responseString.StartsWith("true") == false)
                {
                    modelState.AddModelError(string.Empty, DisplayName.Validation_CaptchaMissing);
                }
            }
        }
        catch (Exception ex)
        {
            log4net.LogManager.GetLogger("WebLogger").Error(string.Format("ValidateReCaptcha failed on {0}/{1}. {2}", controller, action, formValuesRaw), ex);
            modelState.AddModelError(string.Empty, DisplayName.Validation_CaptchaMissing);
        }
    }
}

它在var response = client.PostAsync() 行失败。不总是。我无法在本地复制它。但是对于网站的每个用户来说,它几乎都失败了——有时一次,有时两次,有时更多。最终他们能够注册 - 正如我所说,我看到每小时有 100 多个注册 - 但这会导致该小时的日志表中出现 300-400 个错误。我尝试注册自己 - 虽然我 100% 确定我输入了正确的验证码,但我收到了验证错误。

有什么想法吗?我的验证属性看起来还好吗?还有什么其他原因?

【问题讨论】:

  • 我不认为这是一个 recaptcha 问题
  • 我确定不是 - 我只是用所有细节描述我的问题。正如我所说,它在 PostAsync() 上失败了——知道为什么吗?

标签: c# asp.net-mvc azure recaptcha


【解决方案1】:

啊!..通过删除修复它

using (var client = HttpClientFactory.Create())

一行并将HttpClient 创建为单例:

public class ValidateReCaptchaAttribute : ActionFilterAttribute
{
    private static readonly HttpClient HttpClient = new HttpClient();
    // rest of the code goes here...

this SO answer建议的那样

【讨论】:

  • 我遇到了同样的问题,但我已经使用了 HttpClient 单例(无论如何,MS 建议这样做)。
  • 你能发一个链接到这个“推荐”吗?
  • 这只是我在 5 秒内找到的一个(还有更多,但我没有为它们添加书签):social.msdn.microsoft.com/Forums/en-US/…
  • 看起来 reCAPTCHA 有时会无缘无故拒绝/关闭我的连接。我必须在我的注册表单上添加绕过 reCAPTCHA 验证的代码,以防服务中断。
猜你喜欢
  • 1970-01-01
  • 2019-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-02
  • 1970-01-01
  • 2021-05-12
相关资源
最近更新 更多