SocketException and WebException on getting JSON response for reCAPTCHA

南笙酒味 提交于 2019-12-25 02:22:54

问题


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:


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

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


来源:https://stackoverflow.com/questions/53489379/socketexception-and-webexception-on-getting-json-response-for-recaptcha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!