{"record":{"id":"aa323d18f43139c3","repo":"languagetool-org/languagetool","slug":"got-error-error-http-response-code-conn-ge","errorCode":null,"errorMessage":"Got error: ${error} - HTTP response code ${conn.getResponseCode()}","messagePattern":"Got error: (.+?) - HTTP response code (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"languagetool-http-client/src/main/java/org/languagetool/remote/RemoteLanguageTool.java","lineNumber":226,"sourceCode":"      checkUrl = new URL(serverBaseUrl + V2_MAXTEXTLENGTH);\n    } catch (MalformedURLException e) {\n      throw new RuntimeException(e);\n    }\n    HttpURLConnection conn = getConnection(postData, checkUrl);\n    try {\n      if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {\n        try (InputStream inputStream = conn.getInputStream()) {\n          StringBuilder sb = new StringBuilder();\n          try (InputStreamReader isr = new InputStreamReader(inputStream, \"utf-8\");\n               BufferedReader br = new BufferedReader(isr)) {\n            String line = br.readLine();\n            return Integer.parseInt(line);\n          }\n        }\n      } else {\n        try (InputStream inputStream = conn.getErrorStream()) {\n          String error = readStream(inputStream, \"utf-8\");\n          throw new RuntimeException(\"Got error: \" + error + \" - HTTP response code \" + conn.getResponseCode());\n        }\n      }\n    } catch (ConnectException e) {\n      throw new RuntimeException(\"Could not connect to server at \" + serverBaseUrl, e);\n    } catch (Exception e) {\n      throw new RuntimeException(e);\n    } finally {\n      conn.disconnect();\n    }\n  }\n\n  HttpURLConnection getConnection(byte[] postData, URL url) {\n    try {\n      HttpURLConnection conn = (HttpURLConnection) url.openConnection();\n      conn.setDoOutput(true);\n      conn.setInstanceFollowRedirects(false);\n      conn.setRequestMethod(\"POST\");\n      conn.setRequestProperty(\"Content-Type\", \"application/x-www-form-urlencoded\");","sourceCodeStart":208,"sourceCodeEnd":244,"githubUrl":"https://github.com/languagetool-org/languagetool/blob/2e990059ce67d5e2a0f7f7ca5d31160c6709df4b/languagetool-http-client/src/main/java/org/languagetool/remote/RemoteLanguageTool.java#L208-L244","documentation":"RemoteLanguageTool.getMaxTextLength reads the server's /v2/maxtextlength endpoint; if the HTTP response is not the expected success code, it reads the error stream and throws a RuntimeException containing the server's error body and the HTTP status code. This means the request reached the server but the server rejected it.","triggerScenarios":"Calling getMaxTextLength() against a server that returns a non-2xx response: wrong API endpoint path, server-side authentication failure, an HTTP 4xx/5xx from a proxy, or a non-LanguageTool service listening on the URL.","commonSituations":"Reverse proxy returning 502/503; wrong port pointing at another app; premium API endpoint hit without valid credentials; server overloaded returning errors.","solutions":["Read the embedded error body and HTTP status in the message to identify the server-side problem","Verify serverBaseUrl points at a running LanguageTool server (try /v2/languages in a browser or curl)","Check server logs for the corresponding request failure","If behind a proxy, fix proxy/rate-limiting/auth configuration"],"exampleFix":"// before\nRemoteLanguageTool lt = new RemoteLanguageTool(\"http://localhost:8081\"); // wrong port, another app\n// after\nRemoteLanguageTool lt = new RemoteLanguageTool(\"http://localhost:8081\"); // with LT running:\n// curl -s http://localhost:8081/v2/maxtextlength  -> 200 with number","handlingStrategy":"retry","validationCode":"HttpURLConnection probe = (HttpURLConnection) new URL(serverBaseUrl + \"/v2/languages\").openConnection();\nprobe.setConnectTimeout(3000);\nif (probe.getResponseCode() != 200) throw new IllegalStateException(\"server not healthy\");","typeGuard":"static boolean isLanguageToolServerUp(String baseUrl) {\n  try {\n    HttpURLConnection c = (HttpURLConnection) new URL(baseUrl + \"/v2/languages\").openConnection();\n    c.setConnectTimeout(3000);\n    return c.getResponseCode() == 200;\n  } catch (IOException e) { return false; }\n}","tryCatchPattern":"try {\n  int max = lt.getMaxTextLength();\n} catch (RuntimeException e) {\n  if (e.getMessage().contains(\"Got error:\")) {\n    // parse embedded HTTP code and server body, retry with backoff or fail fast\n  } else throw e;\n}","preventionTips":["Health-check /v2/languages before calling the API","Check server logs whenever the error body/status appears","Bypass or correctly configure proxies for the LT host"],"tags":["java","http","remote-client"],"backgroundTag":"http-error-response","analyzedSha":"2e990059ce67d5e2a0f7f7ca5d31160c6709df4b","analyzedAt":"2026-09-06T09:20:17.015Z","contentChangedAt":"2026-09-06T09:20:17.015Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}