Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ReCAPTCHA使用“ReCAPTCHA”失败很多;现有连接被远程主机强制关闭;错误_C#_Asp.net Mvc_Azure_Recaptcha - Fatal编程技术网

C# ReCAPTCHA使用“ReCAPTCHA”失败很多;现有连接被远程主机强制关闭;错误

C# ReCAPTCHA使用“ReCAPTCHA”失败很多;现有连接被远程主机强制关闭;错误,c#,asp.net-mvc,azure,recaptcha,C#,Asp.net Mvc,Azure,Recaptcha,我正在Azure cloud中托管的MVC4应用程序中使用ReCAPTCHA,用于一个简单的网站和一个注册表。目前,我们每小时大约有100-120次成功注册。问题是,我有数百个System.Net.Sockets.SocketException:日志中的一个现有连接被远程主机强制关闭错误,然后数量继续快速增长: System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> S

我正在Azure cloud中托管的MVC4应用程序中使用ReCAPTCHA,用于一个简单的网站和一个注册表。目前,我们每小时大约有100-120次成功注册。问题是,我有数百个
System.Net.Sockets.SocketException:日志中的一个现有连接被远程主机强制关闭
错误,然后数量继续快速增长:

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)]
公共类ValidateReCaptchaAttribute:ActionFilterAttribute
{
公共覆盖无效OnActionExecuting(ActionExecutingContext filterContext)
{
var formValues=new[]
{
新的KeyValuePair(“privatekey”,ConfigurationProvider.ReCaptchaPrivateKey),
新的KeyValuePair(“remoteip”,remoteip.ToString()),
新的KeyValuePair(“挑战”,challengeField),
新的KeyValuePair(“响应”,响应字段)
};
尝试
{
使用(var client=HttpClientFactory.Create())
使用(var数据=新FormUrlEncodedContent(formValues))
使用(var response=client.PostAsync(“http://www.google.com/recaptcha/api/verify“,数据)。结果)
{
var responseString=response.Content.ReadAsStringAsync().Result;
if(responseString.StartsWith(“true”)==false)
{
modelState.AddModelError(string.Empty,DisplayName.Validation\u CaptchaMissing);
}
}
}
捕获(例外情况除外)
{
log4net.LogManager.GetLogger(“WebLogger”).Error(string.Format(“ValidateReCaptcha在{0}/{1}.{2}上失败”,控制器,操作,formValuesRaw),ex);
modelState.AddModelError(string.Empty,DisplayName.Validation\u CaptchaMissing);
}
}
}
它在
var response=client.PostAsync()
行失败。不总是这样。我无法在本地复制它。但对于网站的每个用户来说,它几乎都失败了——有时一次,有时两次,有时更多。最终他们能够注册——正如我所说,我看到每小时有100多个注册——但这会导致该小时日志表中出现300-400个错误。我试图注册自己-虽然我100%确定我输入的验证码是正确的,但我得到了验证错误


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

啊!。。通过移除来修复它

using (var client = HttpClientFactory.Create())
行并将
HttpClient
创建为单例:

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

正如

所建议的,我不认为这是一个重述问题,我肯定不是——我只是用所有细节描述我的问题。正如我所说,PostAsync()失败-你知道为什么吗?我遇到了同样的问题,但我已经使用了HttpClient singleton(这是微软的建议)。你能发布一个指向这个“建议”的链接吗?这是我在5秒钟内找到的一个链接(还有更多,但我没有将它们标记为书签):看起来reCAPTCHA有时会无缘无故地拒绝/关闭我的连接。我不得不在我的注册表单上添加绕过reCAPTCHA验证的代码,以防服务中断。
public class ValidateReCaptchaAttribute : ActionFilterAttribute
{
    private static readonly HttpClient HttpClient = new HttpClient();
    // rest of the code goes here...