简体   繁体   中英

SocketException and WebException on getting JSON response for reCAPTCHA

I'm getting the following exceptions when getting the JSON response from reCAPTCHA:

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: Unable to connect to the remote server

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

Setting useDefaultCredentials to true in web.config solved the problem:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM