c# - 获取 reCAPTCHA 的 JSON 响应时出现 SocketException 和 WebException
问题描述
从 reCAPTCHA 获取 JSON 响应时,出现以下异常:
SocketException (0x274c):连接尝试失败,因为连接方在一段时间后没有正确响应,或者连接失败,因为连接主机没有响应 172.217.28.228:443
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;
}
解决方案
在 web.config中将 useDefaultCredentials设置为 true 解决了这个问题:
<configuration>
<system.net>
<defaultProxy useDefaultCredentials="true"/>
</system.net>
</configuration>
推荐阅读
- android - 如何将 JSON 拆分为数组?
- php - Sylius 通过特定选项添加产品变体
- php - laravel 与 docker nginx php 响应时间慢 ttfb 120 秒
- apache - Apache Webserver,SSL - 证书:/etc/apache/sites-available 中的 *website*-le-ssl.conf 来自哪里?
- postgresql - 将云托管数据库克隆到本地数据库
- c# - 异步调用已在进行中。必须先完成或取消才能调用此方法
- react-styleguidist - react-styleguidist 配置子组件文档,例如“Form.Item”
- mongodb - 如何将此 MYSQL 查询写入 Mongodb?
- javascript - 从 javascript 对象中获取编码内容并将其显示在 pug 上,如何?
- rss - 如何通过插件处理 Thunderbird 中的提要文件夹?