Recaptcha验证:IOException:尝试全部:1个地址,但无法通过HTTPS连接到服务器:google.com端口:443

时间:2017-11-04 07:47:41

标签: java https recaptcha

我在联系表单中集成了Recaptcha V2。对于服务器端验证,我使用下面的代码:

private JsonObject validateCaptcha(String secret, String response, String remoteip)
  {
      JsonObject jsonObject = null;
      URLConnection connection = null;
      InputStream is = null;
      String charset = Charset.forName("UTF-8").name();
      String url = "https://google.com/recaptcha/api/siteverify";
      try {            
          String query = String.format("secret=%s&response=%s&remoteip=%s", 
          URLEncoder.encode(secret, charset),
          URLEncoder.encode(response, charset),
          URLEncoder.encode(remoteip, charset));

          connection = new URL(url + "?" + query).openConnection();
          is = connection.getInputStream();
          JsonReader rdr = Json.createReader(is);
          jsonObject = rdr.readObject();
      } catch (IOException ex) {
          System.out.println("IOException= "+ex.getMessage());
      }
      finally {
          if (is != null) {
              try {
                  is.close();
              } catch (IOException e) {
                  //cannot do anything here
              }
          }
      }
      return jsonObject;
  }

它抛出IOException并被第一个catch块捕获: 尝试全部:1个地址,但无法通过HTTPS连接到服务器:google.com端口:443

我尝试将链接更改为: https://wwww.google.com/recaptcha/api/siteverify 但同样的错误。 我从localhost运行并在Recaptcha管理控制台中成功添加了“127.0.0.1”作为域。

这个问题可能是什么原因。我知道我们的应用程序是在防火墙后面。

1 个答案:

答案 0 :(得分:1)

我没有在代码中看到ant错误。如果端口443可用,请与网络团队确认。