Ejemplo n.º 1
0
 private String readFromStream(InputStream input) throws IOException {
   BufferedReader reader = null;
   try {
     reader = new BufferedReader(new InputStreamReader(input));
     StringBuffer buffer = new StringBuffer();
     String line = null;
     while ((line = reader.readLine()) != null) {
       buffer.append(line);
     }
     return buffer.toString();
   } finally {
     IOUtilities.close(input);
     IOUtilities.close(reader);
   }
 }
Ejemplo n.º 2
0
  /* (non-Javadoc)
   * @see com.googlecode.flickrjandroid.Transport#sendUpload(java.lang.String, java.util.List)
   */
  @Override
  protected Response sendUpload(String path, List<Parameter> parameters)
      throws IOException, FlickrException, SAXException {

    HttpURLConnection conn = null;
    DataOutputStream out = null;
    String data = null;
    try {
      URL url = UrlUtilities.buildPostUrl(getHost(), getPort(), path);

      conn = (HttpURLConnection) url.openConnection();
      conn.setRequestMethod("POST");
      String boundary = "---------------------------7d273f7a0d3";
      conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
      conn.setRequestProperty("Host", "api.flickr.com");
      conn.setDoInput(true);
      conn.setDoOutput(true);
      conn.connect();
      out = new DataOutputStream(conn.getOutputStream());
      boundary = "--" + boundary;
      out.writeBytes(boundary);
      Iterator<?> iter = parameters.iterator();
      while (iter.hasNext()) {
        Parameter p = (Parameter) iter.next();
        writeParam(p, out, boundary);
      }
      out.writeBytes("--\r\n\r\n");
      out.flush();
      out.close();

      int responseCode = HttpURLConnection.HTTP_OK;
      try {
        responseCode = conn.getResponseCode();
      } catch (IOException e) {
        // logger.error("Failed to get the POST response code", e);
        if (conn.getErrorStream() != null) {
          responseCode = conn.getResponseCode();
        }
      }
      if ((responseCode != HttpURLConnection.HTTP_OK)) {
        String errorMessage = readFromStream(conn.getErrorStream());
        throw new IOException(
            "Connection Failed. Response Code: "
                + responseCode
                + ", Response Message: "
                + conn.getResponseMessage()
                + ", Error: "
                + errorMessage);
      }
      UploaderResponse response = new UploaderResponse();
      Document document = builder.parse(conn.getInputStream());
      response.parse(document);
      return response;
    } finally {
      IOUtilities.close(out);
      if (conn != null) conn.disconnect();
    }
  }
Ejemplo n.º 3
0
  /**
   * Send a GET request to the provided URL with the given parameters, then return the response as a
   * String.
   *
   * @param path
   * @param parameters
   * @return the data in String
   * @throws IOException
   */
  public String getLine(String path, List<Parameter> parameters) throws IOException {
    InputStream in = null;
    BufferedReader rd = null;
    try {
      in = getInputStream(path, parameters);
      rd = new BufferedReader(new InputStreamReader(in, OAuthUtils.ENC));
      final StringBuffer buf = new StringBuffer();
      String line;
      while ((line = rd.readLine()) != null) {
        buf.append(line);
      }

      return buf.toString();
    } finally {
      IOUtilities.close(in);
      IOUtilities.close(rd);
    }
  }
Ejemplo n.º 4
0
  public String sendPost(String path, List<Parameter> parameters) throws IOException {

    HttpURLConnection conn = null;
    DataOutputStream out = null;
    String data = null;
    try {
      URL url = UrlUtilities.buildPostUrl(getHost(), getPort(), path);

      conn = (HttpURLConnection) url.openConnection();
      conn.setRequestMethod("POST");
      String postParam = encodeParameters(parameters);
      byte[] bytes = postParam.getBytes(UTF8);
      conn.setRequestProperty("Content-Length", Integer.toString(bytes.length));
      conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      conn.addRequestProperty("Cache-Control", "no-cache,max-age=0");
      conn.addRequestProperty("Pragma", "no-cache");
      conn.setUseCaches(false);
      conn.setDoOutput(true);
      conn.setDoInput(true);
      conn.connect();
      out = new DataOutputStream(conn.getOutputStream());
      out.write(bytes);
      out.flush();
      out.close();

      int responseCode = HttpURLConnection.HTTP_OK;
      try {
        responseCode = conn.getResponseCode();
      } catch (IOException e) {
        // logger.error("Failed to get the POST response code", e);
        if (conn.getErrorStream() != null) {
          responseCode = conn.getResponseCode();
        }
      }
      if ((responseCode != HttpURLConnection.HTTP_OK)) {
        String errorMessage = readFromStream(conn.getErrorStream());
        throw new IOException(
            "Connection Failed. Response Code: "
                + responseCode
                + ", Response Message: "
                + conn.getResponseMessage()
                + ", Error: "
                + errorMessage);
      }

      String result = readFromStream(conn.getInputStream());
      data = result.trim();
      return data;
    } finally {
      IOUtilities.close(out);
      if (conn != null) conn.disconnect();
    }
  }