opendap/src/main/java/opendap/dap/DConnect2.java
private void openConnection(String urlString, Command command) throws IOException, DAP2Exception
    {
        InputStream is = null;

        try {
            try (HTTPMethod method = HTTPFactory.Get(_session, urlString)) {

                if(acceptCompress)
                    method.setCompression("deflate,gzip");

                // enable sessions
                if(allowSessions)
                    method.setUseSessions(true);
                int statusCode;
                for(; ; ) {
                    statusCode = method.execute();
                    if(statusCode != HttpStatus.SC_SERVICE_UNAVAILABLE)
                        break;
                    Thread.sleep(5000);
                    System.err.println("Service Unavailable");
                }

                // debug
                // if (debugHeaders) ucar.httpservices.HttpClientManager.showHttpRequestInfo(f, method);

                if(statusCode == HttpStatus.SC_NOT_FOUND) {
                    throw new DAP2Exception(DAP2Exception.NO_SUCH_FILE, method.getStatusText() + ": " + urlString);
                }

                if(statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN) {
                    throw new InvalidCredentialsException(method.getStatusText());
                }

                if(statusCode != HttpStatus.SC_OK) {
                    throw new DAP2Exception("Method failed:" + method.getStatusText() + " on URL= " + urlString);
                }

                // Get the response body.
                is = method.getResponseAsStream();

                // check if its an error
                Header header = method.getResponseHeader("Content-Description");
                if(header != null && (header.getValue().equals("dods-error")
                        || header.getValue().equals("dods_error"))) {
                    // create server exception object
                    DAP2Exception ds = new DAP2Exception();
                    // parse the Error object from stream and throw it
                    ds.parse(is);
                    throw ds;
                }

                ver = new ServerVersion(method);

                checkHeaders(method);

                // check for deflator
                Header h = method.getResponseHeader("content-encoding");
                String encoding = (h == null) ? null : h.getValue();
                //if (encoding != null) LogStream.out.println("encoding= " + encoding);

                if(encoding != null && encoding.equals("deflate")) {
                    is = new BufferedInputStream(new InflaterInputStream(is), 1000);
                    if(showCompress) System.out.printf("deflate %s%n", urlString);

                } else if(encoding != null && encoding.equals("gzip")) {
                    is = new BufferedInputStream(new GZIPInputStream(is), 1000);
                    if(showCompress) System.out.printf("gzip %s%n", urlString);
                } else {
                    if(showCompress) System.out.printf("none %s%n", urlString);
                }

                command.process(is);
            }

        } catch (IOException | DAP2Exception e) {
            throw e;

        } catch (Exception e) {
            Util.check(e);
            //e.printStackTrace();
            throw new DAP2Exception(e);
        }
    }
private void openConnection(String urlString, Command command) throws IOException, DAP2Exception
    {
        InputStream is = null;

        try {
            try (HTTPMethod method = HTTPFactory.Get(_session, urlString)) {

                if(acceptCompress)
                    method.setCompression("deflate,gzip");

                // enable sessions
                if(allowSessions)
                    method.setUseSessions(true);
                int statusCode;
                for(; ; ) {
                    statusCode = method.execute();
                    if(statusCode != HttpStatus.SC_SERVICE_UNAVAILABLE)
                        break;
                    Thread.sleep(5000);
                    System.err.println("Service Unavailable");
                }

                // debug
                // if (debugHeaders) ucar.httpservices.HttpClientManager.showHttpRequestInfo(f, method);

                if(statusCode == HttpStatus.SC_NOT_FOUND) {
                    throw new DAP2Exception(DAP2Exception.NO_SUCH_FILE, method.getStatusText() + ": " + urlString);
                }

                if(statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN) {
                    throw new InvalidCredentialsException(method.getStatusText());
                }

                if(statusCode != HttpStatus.SC_OK) {
                    throw new DAP2Exception("Method failed:" + method.getStatusText() + " on URL= " + urlString);
                }

                // Get the response body.
                is = method.getResponseAsStream();

                // check if its an error
                Header header = method.getResponseHeader("Content-Description");
                if(header != null && (header.getValue().equals("dods-error")
                        || header.getValue().equals("dods_error"))) {
                    // create server exception object
                    DAP2Exception ds = new DAP2Exception();
                    // parse the Error object from stream and throw it
                    ds.parse(is);
                    throw ds;
                }

                ver = new ServerVersion(method);

                checkHeaders(method);

                // check for deflator
                Header h = method.getResponseHeader("content-encoding");
                String encoding = (h == null) ? null : h.getValue();
                //if (encoding != null) LogStream.out.println("encoding= " + encoding);

                if(encoding != null && encoding.equals("deflate")) {
                    is = new BufferedInputStream(new InflaterInputStream(is), 1000);
                    if(showCompress) System.out.printf("deflate %s%n", urlString);

                } else if(encoding != null && encoding.equals("gzip")) {
                    is = new BufferedInputStream(new GZIPInputStream(is), 1000);
                    if(showCompress) System.out.printf("gzip %s%n", urlString);
                } else {
                    if(showCompress) System.out.printf("none %s%n", urlString);
                }

                command.process(is);
            }

        } catch (IOException | DAP2Exception e) {
            throw e;

        } catch (Exception e) {
            Util.check(e);
            //e.printStackTrace();
            throw new DAP2Exception(e);
        }
    }
Open a connection to the DODS server.

@param urlString the URL to open; assume already properly encoded
@param command   execute this command on the input stream
@throws IOException   if an IO exception occurred.
@throws DAP2Exception if the DODS server returned an error.