简体   繁体   English

使用HTTPUrlConnection时出现StackOverflowError

[英]StackOverflowError when using HTTPUrlConnection

I'm having some trouble using HTTPUrlConnection. 我在使用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. 该代码基本上在循环中运行,每次都连接到一个不同的URL,检查响应,并在响应满足某些条件时退出。 I'm getting StackOverflowError, but I'm not sure what I have messed up. 我收到StackOverflowError,但是我不确定我搞砸了什么。 I tried without using connection.disconnect() but to no avail. 我尝试不使用connection.disconnect()但无济于事。

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) sun.net.www.http.HttpClient.parseHTTP上java.net.Socket.getInputStream(Socket.java:911)上java.security.AccessController.doPrivileged(本机方法)处的线程“ main”中的java.lang.StackOverflowError异常(HttpClient.java:642)在sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1535)在sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440) Main.sendRequest(Main.java:96)处的Java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)在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. StackOverflowError指示您的sendRequest递归方法调用经常发送到,并且堆栈上没有剩余的内存。 For a nice explanation see What is a StackOverflowError? 有关详细说明,请参阅什么是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. 首先,您可能应该检查一下破坏外观的地方(在handleResponsesendRequest确实按预期工作。特别是handleResponse外观看起来已经连接好了。如果一切正常,则可以尝试重写逻辑以使用循环而不是递归调用。

If you are having a StackOverflow, maybe you are doing something wrong, maybe you need more space for the stack. 如果您有一个StackOverflow,也许您做错了什么,也许您需要更多的堆栈空间。

You can try to increase stack space by means of using this parameter -Xss , for example you could try 1 or 2 megas: -Xss1m . 您可以尝试使用此参数-Xss来增加堆栈空间,例如,可以尝试1或2 -Xss1m-Xss1m

If your problem is bad code, you will get your StackOverflowException, no matter how big your stack size would be. 如果您的问题是代码错误,那么无论堆栈大小如何,都将得到StackOverflowException。 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? 我不是HttpClient专家,但是我可以看到您的sendRequest方法调用了handleResponse方法,而这个又调用了sendRequest ,也许是一个无限循环?

The sendRequest and handleResponse call each other and it's possible they do it for much longer ( more iterations) than you think. sendRequest和handleResponse会相互调用,并且它们执行的时间可能比您想象的更长(更多的迭代)。

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. 如果确实是问题所在(只需记录handleRequest调用sendRequest的次数),则可以为handleResponse建立故障保护/限制,以便在尝试了X次以上之后停止重定向到sendRequest。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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