{"record":{"id":"484c1e0aecea266f","repo":"TooTallNate/Java-WebSocket","slug":"invalid-status-code-received-s-status-line-s","errorCode":null,"errorMessage":"Invalid status code received: %s Status line: %s","messagePattern":"Invalid status code received: (.+?) Status line: (.+?)","errorType":"exception","errorClass":"InvalidHandshakeException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/java_websocket/drafts/Draft.java","lineNumber":168,"sourceCode":"          .format(\"Invalid status line received: %s Status line: %s\", firstLineTokens[2], line));\n    }\n    ClientHandshakeBuilder clienthandshake = new HandshakeImpl1Client();\n    clienthandshake.setResourceDescriptor(firstLineTokens[1]);\n    return clienthandshake;\n  }\n\n  /**\n   * Checking the handshake for the role as client\n   *\n   * @param firstLineTokens the token of the first line split as as an string array\n   * @param line            the whole line\n   * @return a handshake\n   */\n  private static HandshakeBuilder translateHandshakeHttpClient(String[] firstLineTokens,\n      String line) throws InvalidHandshakeException {\n    // translating/parsing the response from the SERVER\n    if (!\"101\".equals(firstLineTokens[1])) {\n      throw new InvalidHandshakeException(String\n          .format(\"Invalid status code received: %s Status line: %s\", firstLineTokens[1], line));\n    }\n    if (!\"HTTP/1.1\".equalsIgnoreCase(firstLineTokens[0])) {\n      throw new InvalidHandshakeException(String\n          .format(\"Invalid status line received: %s Status line: %s\", firstLineTokens[0], line));\n    }\n    HandshakeBuilder handshake = new HandshakeImpl1Server();\n    ServerHandshakeBuilder serverhandshake = (ServerHandshakeBuilder) handshake;\n    serverhandshake.setHttpStatus(Short.parseShort(firstLineTokens[1]));\n    serverhandshake.setHttpStatusMessage(firstLineTokens[2]);\n    return handshake;\n  }\n\n  public abstract HandshakeState acceptHandshakeAsClient(ClientHandshake request,\n      ServerHandshake response) throws InvalidHandshakeException;\n\n  public abstract HandshakeState acceptHandshakeAsServer(ClientHandshake handshakedata)\n      throws InvalidHandshakeException;","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/TooTallNate/Java-WebSocket/blob/afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d/src/main/java/org/java_websocket/drafts/Draft.java#L150-L186","documentation":"During the WebSocket client handshake, translateHandshakeHttpClient parses the HTTP status line returned by the server. The WebSocket protocol requires the server to reply with HTTP 101 Switching Protocols; any other status code makes Draft throw InvalidHandshakeException with this message, so the connection is rejected before it becomes a WebSocket.","triggerScenarios":"Connecting to a server that answers the upgrade request with a non-101 response, e.g. wscat/websocket-client against an endpoint returning 200 OK, 404 Not Found (wrong path), 401/403 (auth failure), 400, or a proxy error page.","commonSituations":"Wrong WebSocket endpoint URL/path, server not supporting WebSocket on that port, reverse proxy or load balancer intercepting the Upgrade request, missing auth headers causing 401, hitting a plain HTTP endpoint instead of a WS endpoint, server rejecting due to missing Origin or subprotocol mismatch.","solutions":["Check the server response (curl -i with Upgrade headers) to see the actual status code and fix the URL/path or server config","Ensure the server endpoint actually performs the WebSocket upgrade (correct library/route enabled)","Verify any reverse proxy (nginx/HAProxy/Apache) forwards Upgrade and Connection headers","Add required auth headers via custom Draft/headers (e.g. via WebSocketClient constructor with HttpHeaders) so the server does not return 401/403","Retry against the correct port and scheme (ws vs wss)"],"exampleFix":"// before (server returns 404)\nWebSocketClient ws = new WebSocketClient(new URI(\"ws://host/api/wrong-path\"));\n// after\nWebSocketClient ws = new WebSocketClient(new URI(\"ws://host/ws/chat\"), new Draft_6455());","handlingStrategy":"validation","validationCode":"// client side: verify endpoint does the upgrade before connecting\nHttpURLConnection c = (HttpURLConnection) new URL(\"http://host/ws/chat\").openConnection();\nc.setRequestProperty(\"Upgrade\", \"websocket\");\nc.setRequestProperty(\"Connection\", \"Upgrade\");\nc.setRequestProperty(\"Sec-WebSocket-Key\", \"dGhlIHNhbXBsZSBub25jZQ==\");\nc.setRequestProperty(\"Sec-WebSocket-Version\", \"13\");\nint status = c.getResponseCode();\nif (status != 101) throw new IllegalStateException(\"Endpoint returned HTTP \" + status + \", not 101\");","typeGuard":null,"tryCatchPattern":"try {\n  client.connectBlocking();\n} catch (WebSocketClient.WebSocketClientException | InterruptedException e) {\n  // InvalidHandshakeException: inspect server HTTP status / auth / proxy\n  logger.error(\"WS handshake rejected: {}\", e.getMessage());\n}","preventionTips":["Confirm the endpoint path/port serves WebSocket upgrades","Configure proxies to pass through Upgrade/Connection headers","Attach auth headers so the server never answers 401/403 to the handshake","Test with curl -i using upgrade headers first"],"tags":["websocket","handshake","http-status"],"backgroundTag":"http-non-200-response","analyzedSha":"afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d","analyzedAt":"2026-09-09T14:39:47.546Z","contentChangedAt":"2026-09-09T14:39:47.546Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}