コード例 #1
0
  /**
   * @param connection that is already connected to its url with an http request, and that should
   *     contain a response for us to retrieve
   * @return a response from the server to the previously submitted http request
   */
  private Response retrieveHtmlResponse(HttpURLConnection connection) {

    Response ret = new Response();

    try {
      ret.setStatusCode(connection.getResponseCode());
      ret.setHeaders(connection.getHeaderFields());
    } catch (Throwable cause) {
      throw new SSEException(cause);
    }

    InputStream inputStream;
    // select the source of the input bytes, first try 'regular' input
    try {
      inputStream = connection.getInputStream();
    }
    // if the connection to the server somehow failed, for example 404 or 500,
    // con.getInputStream() will throw an exception, which we'll keep.
    // we'll also store the body of the exception page, in the response data. */
    catch (Throwable e) {
      inputStream = connection.getErrorStream();
      ret.setFailure(e);
    }

    // this takes data from the previously set stream (error or input)
    // and stores it in a byte[] inside the response
    ByteArrayOutputStream container = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int read;
    try {
      while ((read = inputStream.read(buf, 0, 1024)) > 0) {
        container.write(buf, 0, read);
      }
      ret.setData(container.toByteArray());
    } catch (Exception ex) {
      throw new SSEException(ex);
    }

    return ret;
  }
コード例 #2
0
  private void updateCookies(Response response) {

    Iterable<String> newCookies = response.getHeaders().get(RESTConstants.SET_COOKIE);
    if (newCookies != null) {
      for (String cookie : newCookies) {
        int equalIndex = cookie.indexOf('=');
        int semicolonIndex = cookie.indexOf(';');
        String cookieKey = cookie.substring(0, equalIndex);
        String cookieValue = cookie.substring(equalIndex + 1, semicolonIndex);
        _cookies.put(cookieKey, cookieValue);
      }
    }
  }