{"record":{"id":"db7ee33d9fe616ea","repo":"TooTallNate/Java-WebSocket","slug":"handshake-data-rejected-by-client","errorCode":null,"errorMessage":"Handshake data rejected by client.","messagePattern":"Handshake data rejected by client\\.","errorType":"exception","errorClass":"InvalidHandshakeException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/java_websocket/WebSocketImpl.java","lineNumber":725,"sourceCode":"  @Override\n  public boolean hasBufferedData() {\n    return !this.outQueue.isEmpty();\n  }\n\n  public void startHandshake(ClientHandshakeBuilder handshakedata)\n      throws InvalidHandshakeException {\n    // Store the Handshake Request we are about to send\n    this.handshakerequest = draft.postProcessHandshakeRequestAsClient(handshakedata);\n\n    resourceDescriptor = handshakedata.getResourceDescriptor();\n    assert (resourceDescriptor != null);\n\n    // Notify Listener\n    try {\n      wsl.onWebsocketHandshakeSentAsClient(this, this.handshakerequest);\n    } catch (InvalidDataException e) {\n      // Stop if the client code throws an exception\n      throw new InvalidHandshakeException(\"Handshake data rejected by client.\");\n    } catch (RuntimeException e) {\n      log.error(\"Exception in startHandshake\", e);\n      wsl.onWebsocketError(this, e);\n      throw new InvalidHandshakeException(\"rejected because of \" + e);\n    }\n\n    // Send\n    write(draft.createHandshake(this.handshakerequest));\n  }\n\n  private void write(ByteBuffer buf) {\n    log.trace(\"write({}): {}\", buf.remaining(),\n        buf.remaining() > 1000 ? \"too big to display\" : new String(buf.array()));\n\n    outQueue.add(buf);\n    wsl.onWriteDemand(this);\n  }\n","sourceCodeStart":707,"sourceCodeEnd":743,"githubUrl":"https://github.com/TooTallNate/Java-WebSocket/blob/afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d/src/main/java/org/java_websocket/WebSocketImpl.java#L707-L743","documentation":"In startHandshake (client role), the library first calls wsl.onWebsocketHandshakeSentAsClient(); if the application's listener throws InvalidDataException, the library converts it into InvalidHandshakeException('Handshake data rejected by client.') and aborts the WebSocket opening handshake. It means your own handshake-verification callback rejected the outgoing request.","triggerScenarios":"Implementing onWebsocketHandshakeSentAsClient in a WebSocketClient subclass and throwing InvalidDataException from it (e.g. rejecting missing headers), then connecting.","commonSituations":"Custom header validation/authorization checks in the handshake callback; strict verification of the handshake request that fails because a required header or token is absent.","solutions":["Inspect your onWebsocketHandshakeSentAsClient implementation — remove or fix the InvalidDataException it throws.","Ensure required handshake headers/tokens are set before connect().","If rejection is intended, handle InvalidHandshakeException from connect()/onOpen failure gracefully.","If you need error detail, log inside the callback before throwing."],"exampleFix":"// before\n@Override\npublic void onWebsocketHandshakeSentAsClient(WebSocket conn, ClientHandshake request) throws InvalidDataException {\n  throw new InvalidDataException(1006, \"no token\");\n}\n// after\n@Override\npublic void onWebsocketHandshakeSentAsClient(WebSocket conn, ClientHandshake request) throws InvalidDataException {\n  // validate or accept; only throw when genuinely invalid\n}","handlingStrategy":"try-catch","validationCode":"// validate handshake inputs before connecting\nif (authToken == null) throw new IllegalStateException(\"token must be set before connect()\");","typeGuard":null,"tryCatchPattern":"try { client.connectBlocking(); } catch (InvalidHandshakeException e) {\n  log.error(\"handshake rejected: \" + e.getMessage());\n}","preventionTips":["Only throw InvalidDataException from onWebsocketHandshakeSentAsClient for genuinely invalid handshakes","Set required headers/tokens before connect()","Log inside the callback to diagnose rejections"],"tags":["handshake","client","callback"],"backgroundTag":"handshake-rejected","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"}