java.net.ConnectException连接超时:connect

时间:2017-03-24 18:50:29

标签: java rest ejb

我正在使用Java EE7中的RESTFul api。我正在使用@Stateless EJB和一个我可以通过HTTP Post请求到达的方法。我正在尝试验证服务器端的重新访问代码,因此我需要使用我的密钥和客户端发送的google验证码响应向https://www.google.com/recaptcha/api/siteverify/发出HTTP Post请求。

当我尝试从服务器发出请求时,会出现问题。 ConnectException是一个例外,消息“Connection timed out:connect”。我想我无法连接到网络,但我不确定。我正在使用NetBeans IDE 8.1

以下是代码:

@Stateless
@Path("sign-in")
public class SignIn 
{

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public javax.ws.rs.core.Response registerUser(String data) throws IOException, JSONException
    {
        JSONObject JObj = new JSONObject(data);
        String gCaptchaResponse = JObj.getString("$captchaResponse");

        if (!VerifyGCaptchaResponse(gCaptchaResponse))
    {
        //Response with error
    }

    //Logic to "sign-in"
    //...
     }


    private final String urlverificacion = "https://www.google.com/recaptcha/api/siteverify/";

    private boolean VerifyGCaptchaResponse(String GCResponse) throws JSONException
    {
        boolean res = false;

        String paramSecret = "secret="+appSettings.getCaptchaSecretKey();
        String paramResponse = "response="+GCResponse;
        String urlparameters = paramSecret + "&" + paramResponse;
        byte[] postData = urlparameters.getBytes( StandardCharsets.UTF_8 );
        int postDataLength = postData.length;

        HttpURLConnection connection = null;

        try 
        {
            URL url = new URL(urlverificacion);
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty( "charset", "utf-8");
            connection.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
            connection.setConnectTimeout(30000);
            connection.setReadTimeout(30000);

            //Make request
            OutputStream os = connection.getOutputStream(); // <<<<<<===== Here is when the problem occurs!!!
            os.write(postData);
            os.flush();

            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
            {
                throw new RuntimeException("Fallo conexion : codigo de error HTTP : " + connection.getResponseCode());
            }

            StringBuilder builder = new StringBuilder();
            BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream())));

            String output;
            System.out.println("Respuesta del servicio .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
                builder.append(output);
            }

            String idSicom = "0";
            Integer id = 0;
            JSONObject jResponse = new JSONObject(builder.toString());
            if (jResponse.has("success")) 
            {
                String success = jResponse.getString("success");

                res = true;
            } 
            else 
            {

            }

            connection.disconnect();

        } 
        catch (java.net.ConnectException e)
        {
            String error = e.getMessage();
            String cause = Arrays.toString(e.getStackTrace());

        }
        catch (IOException | RuntimeException | JSONException e) 
        {
            String error = e.getMessage();
            String cause = Arrays.toString(e.getStackTrace());

        } 
        finally 
        {

        }

        return res;
    }

}

0 个答案:

没有答案