本文介绍了SocketException和WebException关于获取reCAPTCHA的JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从reCAPTCHA获得JSON响应时,出现以下异常:
I'm getting the following exceptions when getting the JSON response from reCAPTCHA:
SocketException(0x274c):连接尝试失败,因为一段时间后连接方未正确响应,或者由于连接的主机未能响应172.217.28.228:443 ,建立的连接失败
SocketException (0x274c): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 172.217.28.228:443
WebException:无法连接到远程服务器
我的服务器端验证如下:
My server-side validation is as follows:
string secretKey = ConfigurationManager.AppSettings["Captcha.SecretKey"];
string url = "https://www.google.com/recaptcha/api/siteverify?secret=" + secretKey + "&response=" + response;
try
{
using (WebClient wc = new WebClient())
{
string jsonResponse = wc.DownloadString(url); //The exception happens here
CaptchaResponse captchaResponse = (new JavaScriptSerializer()).Deserialize<CaptchaResponse>(jsonResponse);
return Convert.ToBoolean(captchaResponse.Success);
}
}
catch (Exception ex)
{
throw ex;
}
推荐答案
在web.config中将 useDefaultCredentials 设置为true解决了该问题:
Setting useDefaultCredentials to true in web.config solved the problem:
<configuration>
<system.net>
<defaultProxy useDefaultCredentials="true"/>
</system.net>
</configuration>
这篇关于SocketException和WebException关于获取reCAPTCHA的JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!