SocketException和WebException关于获取reCAPTCHA的JSON响应

时间:2018-11-26 21:31:14

标签: c# asp.net recaptcha

从reCAPTCHA获得JSON响应时,出现以下异常:

SocketException(0x274c):连接尝试失败,因为一段时间后连接方未正确响应,或者由于连接的主机未能响应172.217.28.228:443 ,建立的连接失败 < / p>

WebException:无法连接到远程服务器

我的服务器端验证如下:

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;
}

1 个答案:

答案 0 :(得分:0)

在web.config中将 useDefaultCredentials 设置为true解决了该问题:

<configuration>
  <system.net>
    <defaultProxy useDefaultCredentials="true"/>
  </system.net>
</configuration>