{"action":"created","issue":{"url":"https://api.github.com/repos/TakahikoKawasaki/nv-websocket-client/issues/27","labels_url":"https://api.github.com/repos/TakahikoKawasaki/nv-websocket-client/issues/27/labels{/name}","comments_url":"https://api.github.com/repos/TakahikoKawasaki/nv-websocket-client/issues/27/comments","events_url":"https://api.github.com/repos/TakahikoKawasaki/nv-websocket-client/issues/27/events","html_url":"https://github.com/TakahikoKawasaki/nv-websocket-client/issues/27","id":126422689,"number":27,"title":"Authentication issue ","user":{"login":"raviinder","id":16685562,"avatar_url":"https://avatars.githubusercontent.com/u/16685562?v=3","gravatar_id":"","url":"https://api.github.com/users/raviinder","html_url":"https://github.com/raviinder","followers_url":"https://api.github.com/users/raviinder/followers","following_url":"https://api.github.com/users/raviinder/following{/other_user}","gists_url":"https://api.github.com/users/raviinder/gists{/gist_id}","starred_url":"https://api.github.com/users/raviinder/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/raviinder/subscriptions","organizations_url":"https://api.github.com/users/raviinder/orgs","repos_url":"https://api.github.com/users/raviinder/repos","events_url":"https://api.github.com/users/raviinder/events{/privacy}","received_events_url":"https://api.github.com/users/raviinder/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":9,"created_at":"2016-01-13T13:55:32Z","updated_at":"2016-01-26T18:40:06Z","closed_at":"2016-01-26T18:40:06Z","body":"Hi , \r\n    When i am trying to connect to my server which requires username/password.\r\n It respond me with \r\n**********************************************\r\nHTTP Version  = HTTP/1.1\r\nStatus Code   = 401\r\nReason Phrase = Unauthorized\r\n*************************************************\r\n\r\n1) Do we have any kind of function by which we can add username/password to any initial connect to Websocket server ?\r\n\r\n\r\nHere is my piece of code \r\n        try {\r\n        \t\r\n\t\t        HostnameVerifier hostnameVerifier = new HostnameVerifier() {\r\n\t\t\t    @Override\r\n\t\t\t    public boolean verify(String hostname, SSLSession session) {\r\n\t\t\t        return true;\r\n\t\t\t    }\r\n\t\t\t};\r\n      \t TrustManager[] trustAllCerts = new TrustManager[]{\r\n\t               new X509TrustManager() {\r\n\t               public X509Certificate[] getAcceptedIssuers() {\r\n\t                         X509Certificate[] myTrustedAnchors = new X509Certificate[0];\r\n\t\t                    return myTrustedAnchors;\r\n\t\t              }\r\n\t\t            @Override\r\n\t\t              public void checkClientTrusted(X509Certificate[] certs, String authType) {\r\n\t\t               }\r\n\t\t             @Override\r\n\t\t               public void checkServerTrusted(X509Certificate[] certs, String authType) {\r\n\t\t                }\r\n\t\t           }\r\n\t\t        };\r\n            \r\n            // Create an SSLContext that uses our TrustManager\r\n            \r\n\t\t\ttry {\r\n\t\t\t\tsc = SSLContext.getInstance(\"SSL\");\r\n\t\t\t\t sc.init(null, trustAllCerts, null);\r\n\t\t\t} catch (NoSuchAlgorithmException | KeyManagementException e) {\r\n\t\t\t\t// TODO Auto-generated catch block\r\n\t\t\t\te.printStackTrace();\r\n\t\t\t}\r\n           \r\n            Authenticator.setDefault(new Authenticator(){\r\n                protected PasswordAuthentication getPasswordAuthentication() {\r\n                \tString  username= \"su\";\r\n                \tString passsetN=\"cws\";\r\n                    return new PasswordAuthentication(username,passsetN.toCharArray());\r\n                }});\r\n\r\n        \t\r\n        \tWebSocketFactory factory = new WebSocketFactory();\r\n        \tfactory.setSSLSocketFactory(sc.getSocketFactory());\r\n        \tfactory.createSocket(\"https://192.168.0.8/notification/echo.html\")\r\n\t\t\t    .addListener(new WebSocketAdapter() {\r\n\t\t\t        @Override\r\n\t\t\t        public void onTextMessage(WebSocket ws, String message) {\r\n\t\t\t            // Received a response. Print the received message.\r\n\t\t\t            System.out.println(message);\r\n\r\n\t\t\t            // Close the WebSocket connection.\r\n\t\t\t            ws.disconnect();\r\n\t\t\t        }\r\n\t\t\t    })\r\n\t\t\t    .connect()\r\n\t\t\t    .sendText(\"Hello.\");\r\n\t\t} catch ( IOException e) {\r\n\t\t\t// TODO Auto-generated catch block\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n        catch (OpeningHandshakeException e)\r\n        {\r\n            // Status line.\r\n            StatusLine sl = e.getStatusLine();\r\n            System.out.println(\"=== Status Line ===\");\r\n            System.out.format(\"HTTP Version  = %s\\n\", sl.getHttpVersion());\r\n            System.out.format(\"Status Code   = %d\\n\", sl.getStatusCode());\r\n            System.out.format(\"Reason Phrase = %s\\n\", sl.getReasonPhrase());\r\n\r\n            // HTTP headers.\r\n            Map<String, java.util.List<String>> headers = e.getHeaders();\r\n            System.out.println(\"=== HTTP Headers ===\");\r\n            for (Entry<String, java.util.List<String>> entry : headers.entrySet())\r\n            {\r\n                // Header name.\r\n                String name = entry.getKey();\r\n\r\n                // Values of the header.\r\n                List values = (List) entry.getValue();\r\n\r\n                //if (values == null || values.size() == 0)\r\n      //          {\r\n                    // Print the name only.\r\n                    System.out.println(name);\r\n                    continue;\r\n                }\r\n\r\n    //            for (String value : values)\r\n                {\r\n                    // Print the name and the value.\r\n                //    System.out.format(\"%s: %s\\n\", name, value);\r\n                }\r\n            }\r\n\r\n**************************************************************"},"comment":{"url":"https://api.github.com/repos/TakahikoKawasaki/nv-websocket-client/issues/comments/175165934","html_url":"https://github.com/TakahikoKawasaki/nv-websocket-client/issues/27#issuecomment-175165934","issue_url":"https://api.github.com/repos/TakahikoKawasaki/nv-websocket-client/issues/27","id":175165934,"user":{"login":"raviinder","id":16685562,"avatar_url":"https://avatars.githubusercontent.com/u/16685562?v=3","gravatar_id":"","url":"https://api.github.com/users/raviinder","html_url":"https://github.com/raviinder","followers_url":"https://api.github.com/users/raviinder/followers","following_url":"https://api.github.com/users/raviinder/following{/other_user}","gists_url":"https://api.github.com/users/raviinder/gists{/gist_id}","starred_url":"https://api.github.com/users/raviinder/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/raviinder/subscriptions","organizations_url":"https://api.github.com/users/raviinder/orgs","repos_url":"https://api.github.com/users/raviinder/repos","events_url":"https://api.github.com/users/raviinder/events{/privacy}","received_events_url":"https://api.github.com/users/raviinder/received_events","type":"User","site_admin":false},"created_at":"2016-01-26T18:40:06Z","updated_at":"2016-01-26T18:40:06Z","body":"Thank you .. that was really helpful. \r\n But i have one more question do we have "}}