来自Java的Google Recaptcha由于连接超时而失败

时间:2018-10-12 22:10:46

标签: java ssl recaptcha httpsurlconnection

我正在尝试在我的应用程序中添加google recaptcha。以下是验证Google验证码的代码。

尝试{

            URL obj = new URL("https://www.google.com/recaptcha/api/siteverify");
            HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
            Properties properties = System.getProperties();
            // add reuqest header
            con.setRequestMethod("POST");
            con.setRequestProperty("User-Agent", USER_AGENT);
            con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
            con.setConnectTimeout(30 * 1000);
             con.setReadTimeout(30 * 1000);

            String postParams = "secret=" + secret + "&response="
                    + gRecaptchaResponse;


            if (MyConstants.IS_PROXY_ENABLED) {
                properties.put("http.proxyHost",MyConstants.HTTP_PROXY_HOST);
                properties.put("http.proxyPort",MyConstants.HTTP_PROXY_PORT);
                String authString = MyConstants.HTTP_PROXY_AUTHENTICATION;

                String encodedAuthString = "Basic "
                        + new sun.misc.BASE64Encoder().encode(authString.getBytes());

                con.setRequestProperty("Proxy-authorization",encodedAuthString);

            }

            // Send post request
            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            wr.writeBytes(postParams);
            wr.flush();
            wr.close();

            int responseCode = con.getResponseCode();
            System.out.println("\nSending 'POST' request to URL : " + SITE_VERIFY_URL);
            System.out.println("Post parameters : " + postParams);
            System.out.println("Response Code : " + responseCode);

            BufferedReader in = new BufferedReader(new InputStreamReader(
                    con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // print result
            System.out.println(response.toString());

            //parse JSON response and return 'success' value
            JsonReader jsonReader = Json.createReader(new StringReader(response.toString()));
            JsonObject jsonObject = jsonReader.readObject();
            jsonReader.close();

            return jsonObject.getBoolean("success");
            }catch(Exception e){
                e.printStackTrace();
                return false;
            }

每次都失败,并出现连接超时错误。我试图设置超时时间,但没有帮助。

Connection timed out: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:519)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:550)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:158)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
    at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:271)
    at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:328)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:172)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:778)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:158)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:881)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:230)

当我尝试使用输入参数从浏览器访问https://www.google.com/recaptcha/api/siteverify时,会给我结果

 {
  "success": true,
  "challenge_ts": "2018-10-12T22:07:18Z",
  "hostname": "localhost"
}

有人可以帮助我为什么连接超时错误来自代码吗?

0 个答案:

没有答案