简体   繁体   中英

StackOverflowError when using HTTPUrlConnection

I'm having some trouble using HTTPUrlConnection. The code is basically running in a loop, connecting to a different URL each time, checking the response, and quitting if the response meets some criteria. I'm getting StackOverflowError, but I'm not sure what I have messed up. I tried without using connection.disconnect() but to no avail.

Also, do you guys have any tips on speeding up the code below?

Thanks

Exception in thread "main" java.lang.StackOverflowError at java.security.AccessController.doPrivileged(Native Method) at java.net.Socket.getInputStream(Socket.java:911) at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:642) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1535) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440) at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480) at Main.sendRequest(Main.java:96) at Main.handleResponse(Main.java:140)

public class Main {
    static int counter;

    public static void main(String [] args) {
        counter = 0;
        sendRequest("http://192.168.0.1");
    }

    public static void sendRequest(String path) {
        try {
            URL url = new URL(path);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();

            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream());

                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                StringBuilder stringBuilder = new StringBuilder();
                String line = null;
                while ((line = bufferedReader.readLine()) != null) {
                    stringBuilder.append(line);
                }

                String response = stringBuilder.toString();
                handleResponse(response);
                inputStreamReader.close();
                bufferedReader.close();
            }
            connection.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void handleResponse(String response) {
        String s = response.substring(62,67);
        if (s.equals("Statu")) {
            return;
        } else {
            sendRequest("http://192.168.0.1/" + counter);
            counter += 1;
        }
    }
}

A StackOverflowError indicates that your recursive method call of sendRequest was sent to often, and there is no more memory left on the Stack. For a nice explanation see What is a StackOverflowError? First you should probably check if the places where you break the look (in handleResponse and sendRequest do work exactly as you expect. Especially the one in handleResponse looks wired. If all that is as it should be you could try to rewrite your logic to use a loop instead of a recursive call.

If you are having a StackOverflow, maybe you are doing something wrong, maybe you need more space for the stack.

You can try to increase stack space by means of using this parameter -Xss , for example you could try 1 or 2 megas: -Xss1m .

If your problem is bad code, you will get your StackOverflowException, no matter how big your stack size would be. I am not an expert oh HttpClient, but I can see your sendRequest method calls your handleResponse method, and this one calls again sendRequest , maybe an infinite loop?

The sendRequest and handleResponse call each other and it's possible they do it for much longer ( more iterations) than you think.

If that is indeed the issue ( just log how many times handleRequest invokes sendRequest) you could build a failsafe/limit for handleResponse to stop redirecting to sendRequest if more than X tries have been made.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM