/**
   * returns the inputstream from URLConnection
   *
   * @return InputStream
   */
  public InputStream getInputStream() {
    try {
      int responseCode = this.urlConnection.getResponseCode();

      try {
        // HACK: manually follow redirects, for the login to work
        // HTTPUrlConnection auto redirect doesn't respect the provided headers
        if (responseCode == 302) {
          HttpClient redirectClient =
              new HttpClient(
                  proxyHost,
                  proxyPort,
                  urlConnection.getHeaderField("Location"),
                  headers,
                  urlConnection.getRequestMethod(),
                  callback);
          redirectClient.getInputStream().close();
        }
      } catch (Throwable e) {
        System.out.println("Following redirect failed");
      }

      setCookieHeader = this.urlConnection.getHeaderField("Set-Cookie");

      InputStream in =
          responseCode != HttpURLConnection.HTTP_OK
              ? this.urlConnection.getErrorStream()
              : this.urlConnection.getInputStream();

      return in;
    } catch (Exception e) {
      return null;
    }
  }
  /**
   * @param phost PROXY host name
   * @param pport PROXY port string
   * @param url URL string
   * @param headers Map
   */
  public HttpClient(
      String phost,
      int pport,
      String url,
      Map headers,
      String method,
      XmlHttpProxy.CookieCallback callback)
      throws MalformedURLException {
    this.callback = callback;

    if (phost != null && pport != -1) {
      this.isProxy = true;
    }

    this.proxyHost = phost;
    this.proxyPort = pport;

    if (url.trim().startsWith("https:")) {
      isHttps = true;
    }

    this.urlConnection = getURLConnection(url);
    try {
      this.urlConnection.setRequestMethod(method);
    } catch (java.net.ProtocolException pe) {
      HttpClient.getLogger().severe("Unable protocol method to " + method + " : " + pe);
    }
    this.headers = headers;
    writeHeaders(headers);
  }
  /**
   * @param phost PROXY host name
   * @param pport PROXY port string
   * @param url URL string
   * @param headers Map
   * @param userName string
   * @param password string
   */
  public HttpClient(
      String phost,
      int pport,
      String url,
      Map headers,
      String method,
      String userName,
      String password,
      XmlHttpProxy.CookieCallback callback)
      throws MalformedURLException {

    this.callback = callback;

    try {
      if (phost != null && pport != -1) {
        this.isProxy = true;
      }

      this.proxyHost = phost;
      this.proxyPort = pport;
      if (url.trim().startsWith("https:")) {
        isHttps = true;
      }
      this.urlConnection = getURLConnection(url);
      try {
        this.urlConnection.setRequestMethod(method);
      } catch (java.net.ProtocolException pe) {
        HttpClient.getLogger().severe("Unable protocol method to " + method + " : " + pe);
      }
      // set basic authentication information
      String auth = userName + ":" + password;
      String encoded = new sun.misc.BASE64Encoder().encode(auth.getBytes());
      // set basic authorization
      this.urlConnection.setRequestProperty("Authorization", "Basic " + encoded);
      this.headers = headers;
      writeHeaders(headers);
    } catch (Exception ex) {
      HttpClient.getLogger()
          .severe("Unable to set basic authorization for " + userName + " : " + ex);
    }
  }