Skip to content

Custom HTTP headers using while establishing Websocket connection

Since Android SDK build 1.1.0.16 it is possible to add custom HTTP headers while establishing Websocket connection to WCS server. To do this, use the following function

Session.connect(Connection connection, java.util.Map<java.lang.String,java.lang.String> customHeaders)

Suppose that Websocket traffic proxying with user authentication is configured on the server using nginx. In this case, user credentials should be passed in Websocket URL

wss://login:password@test.flashphoner.com/wss

Authorization HTTP header should be formed from Websocket URL as follows:

code

private Map<String, String> getBasicAuthHeader(String url) {
    if (url.contains("@")) {
        String authorization = url.substring(url.indexOf(":")+3, url.indexOf("@"));
        String base64Auth = Base64.getEncoder().encodeToString(authorization.getBytes());
        String header = "Basic " + base64Auth;
        Map<String, String> basicAuthheader = new HashMap<>();
        basicAuthheader.put("Authorization", header);

        return basicAuthheader;
    }

    return null;
}

and should be sent while connecting to the server

code

session.connect(new Connection(), getBasicAuthHeader(url));