【发布时间】: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