HttpURLConnection schließen

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • HttpURLConnection schließen

    Ich schicke innerhalb von wenigen Sekunden mehrere Kommandos an einen Server. Für jedes Kommando mache ich eine neue Verbindung auf und zu. Das Problem ist, wenn ich disconnect() aufrufe, bleibt die Verbindung trotzdem offen!

    Ich habe am Schluss einer Kurzversion der Sendungen 16 offene Verbindungen! Würde ich die "Vollversion" schicken, wären das mehrere hundert Kommandos, aus dem resultierend auch mehrere hundert offene Verbindungen!

    Ich weiß ehrlich gesagt nicht mehr, warum die Verbindungen offen bleiben, hat da jemand eine Idee?

    Die nachstehende Funktion ist für das Senden der Kommandos zuständig!

    Quellcode

    1. void confUpdate() throws Exception
    2. {
    3. String data = "";
    4. for (Map.Entry<String,String> e: confUpdate.entrySet()) {
    5. if (data.length() > 0)
    6. data += "&";
    7. data += URLEncoder.encode(e.getKey(), "UTF-8") + "=" + URLEncoder.encode(e.getValue(), "UTF-8");
    8. }
    9. URL url = new URL("http", host, port, "/cgi/conf");
    10. HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    11. conn.setUseCaches(false);
    12. conn.setConnectTimeout(7000);
    13. conn.setReadTimeout(7000);
    14. conn.setDoOutput(true);
    15. if ((user != null) && (password != null)) {
    16. String auth;
    17. auth = user + ":" + password;
    18. auth = Base64.encodeToString(auth.getBytes("UTF-8"), false);
    19. conn.setRequestProperty("Authorization", "Basic " + auth);
    20. }
    21. {
    22. OutputStreamWriter outstrwr = new OutputStreamWriter(conn.getOutputStream());
    23. outstrwr.write(data);
    24. outstrwr.flush();
    25. outstrwr.close();
    26. conn.disconnect();
    27. }
    28. confUpdate.clear();
    29. }
    Alles anzeigen
    Never walk alone! Have someone to keep your back!