0

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;
        }
    }
}
tsorn
  • 3,365
  • 1
  • 29
  • 48
  • 1
    There is no loop here, but there is a recursion, hence the stack overflow. Try writing the loop you claim already exists. – user207421 Sep 29 '15 at 11:23
  • 1
    your counter is never increased. The recursive call is made before the counter is incremented. Unless the response code is not HTTP_OK (or an exception occurs) – David ten Hove Sep 29 '15 at 11:43

3 Answers3

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.

Community
  • 1
  • 1
hotzst
  • 7,238
  • 9
  • 41
  • 64
1

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?

malaguna
  • 4,183
  • 1
  • 17
  • 33
  • Yes! It's not an infinite loop, but it should be able to handle a couple of million "loops". I'll try to switch from a recursive call to a loop instead and see what happens. Thanks! – tsorn Sep 29 '15 at 11:25
1

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.