SocketException and WebException on getting JSON r

2019-08-24 22:38发布

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

1条回答
干净又极端
2楼-- · 2019-08-24 23:25

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

<configuration>
  <system.net>
    <defaultProxy useDefaultCredentials="true"/>
  </system.net>
</configuration>
查看更多
登录 后发表回答