Example #1
0
 public static String encodeParameters(List<Parameter> parameters) {
   if (parameters == null || parameters.isEmpty()) {
     return "";
   }
   StringBuffer buf = new StringBuffer();
   for (int i = 0; i < parameters.size(); i++) {
     if (i != 0) {
       buf.append("&");
     }
     Parameter param = parameters.get(i);
     buf.append(UrlUtilities.encode(param.getName()))
         .append("=")
         .append(UrlUtilities.encode(String.valueOf(param.getValue())));
   }
   return buf.toString();
 }
Example #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();
    }
  }
Example #3
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();
    }
  }
Example #4
0
  private InputStream getInputStream(String path, List<Parameter> parameters) throws IOException {
    URL url = UrlUtilities.buildUrl(getHost(), getPort(), path, parameters);

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.addRequestProperty("Cache-Control", "no-cache,max-age=0");
    conn.addRequestProperty("Pragma", "no-cache");
    conn.setRequestMethod("GET");
    if (proxyAuth) {
      conn.setRequestProperty("Proxy-Authorization", "Basic " + getProxyCredentials());
    }
    conn.connect();
    return conn.getInputStream();
  }