@Override
    public void close() throws IOException {
      bout.close();
      output.close();

      Iterator<MessageHandler> it = config.getMessagerHandlers();

      while (it.hasNext()) {
        MessageHandler handler = it.next();
        handler.handleRequest(url, bout.toByteArray());
      }
    }
  @Override
  public InputStream getContent() throws IOException {
    InputStream in;

    try {
      successful = true;
      in = connection.getInputStream();
    } catch (IOException e) {
      successful = false;
      in = connection.getErrorStream();
      if (in == null) {
        throw e;
      }
    }

    String encoding = connection.getHeaderField("Content-Encoding");

    if (config.getMaxResponseSize() > 0) {
      in = new LimitingInputStream(config.getMaxResponseSize(), in);
    }

    if ("gzip".equals(encoding)) {
      in = new GZIPInputStream(in);
    }

    if (config.hasMessageHandlers() || config.isTraceMessage()) {
      byte[] bytes = FileUtil.toBytes(in);
      in = new ByteArrayInputStream(bytes);

      if (config.hasMessageHandlers()) {
        Iterator<MessageHandler> it = config.getMessagerHandlers();
        while (it.hasNext()) {
          MessageHandler handler = it.next();
          if (handler instanceof MessageHandlerWithHeaders) {
            ((MessageHandlerWithHeaders) handler)
                .handleResponse(url, bytes, connection.getHeaderFields());
          } else {
            handler.handleResponse(url, bytes);
          }
        }
      }

      if (config.isTraceMessage()) {
        new TeeInputStream(config, bytes);
      }
    }

    return in;
  }
  private InputStream doHttpGet(URL url) throws IOException, AsyncApiException {
    HttpURLConnection connection = JdkHttpTransport.createConnection(config, url, null);
    connection.setRequestProperty(SESSION_ID, config.getSessionId());

    boolean success = true;
    InputStream in;
    try {
      in = connection.getInputStream();
    } catch (IOException e) {
      success = false;
      in = connection.getErrorStream();
    }

    String encoding = connection.getHeaderField("Content-Encoding");
    if ("gzip".equals(encoding)) {
      in = new GZIPInputStream(in);
    }

    if (config.isTraceMessage() || config.hasMessageHandlers()) {
      byte[] bytes = FileUtil.toBytes(in);
      in = new ByteArrayInputStream(bytes);

      if (config.hasMessageHandlers()) {
        for (MessageHandler handler : config.getMessagerHandlers()) {
          if (handler instanceof MessageHandlerWithHeaders) {
            ((MessageHandlerWithHeaders) handler).handleRequest(url, new byte[0], null);
            ((MessageHandlerWithHeaders) handler)
                .handleResponse(url, bytes, connection.getHeaderFields());
          } else {
            handler.handleRequest(url, new byte[0]);
            handler.handleResponse(url, bytes);
          }
        }
      }

      if (config.isTraceMessage()) {
        config.getTraceStream().println(url.toExternalForm());

        Map<String, List<String>> headers = connection.getHeaderFields();
        for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
          StringBuffer sb = new StringBuffer();
          List<String> values = entry.getValue();

          if (values != null) {
            for (String v : values) {
              sb.append(v);
            }
          }

          config.getTraceStream().println(entry.getKey() + ": " + sb.toString());
        }

        new XmlTraceHelper(config.getTraceStream(), config.isPrettyPrintXml()).trace(bytes);
      }
    }

    if (!success) {
      parseAndThrowException(in);
    }

    return in;
  }