Esempio n. 1
0
 @Override
 public boolean postTurnSummary(
     final GameData gameData, final String turnSummary, final String player, final int round) {
   try {
     final List<Part> parts = new ArrayList<>();
     parts.add(createStringPart("siteid", m_siteId));
     if (gameData != null) {
       parts.add(createStringPart("production", getProductionData(gameData)));
     }
     parts.add(createStringPart("gamename", m_gameName));
     parts.add(createStringPart("player", player));
     parts.add(createStringPart("summary", turnSummary));
     parts.add(createStringPart("round", "" + round));
     parts.add(createStringPart("sendmail", m_mailSaveGame ? "true" : "false"));
     if (m_saveGameFile != null) {
       final FilePart part = new FilePart("userfile", m_saveGameFileName, m_saveGameFile);
       part.setContentType("application/octet-stream");
       parts.add(part);
     }
     m_serverMessage = executePost(m_host, "upload.php", parts);
     if (!m_serverMessage.toLowerCase().contains("success")) {
       System.out.println("Unknown error, site response: " + m_serverMessage);
       return false;
     }
   } catch (final Exception e) {
     m_serverMessage = e.getMessage();
     ClientLogger.logQuietly(e);
     return false;
   }
   return true;
 }
Esempio n. 2
0
 public static String post(String path, ChatMessageBean cm, FormFile file) throws Exception {
   MultipartPostMethod filePost = new MultipartPostMethod(path);
   FilePart cbFile = new FilePart(file.getFileName(), file.getFile());
   cbFile.setContentType("audio/" + file.getParameterName());
   filePost.addParameter("message", new JSONObject().toJSONString(cm));
   filePost.addParameter("filename", file.getFileName());
   filePost.addParameter("uploadFiles", file.getFileName(), file.getFile());
   filePost.addPart(cbFile);
   filePost
       .getParams()
       .setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
   HttpClient client = new HttpClient();
   int statusInt = client.executeMethod(filePost);
   System.out.println("statusInt:" + statusInt);
   return filePost.getResponseBodyAsString();
 }
  /**
   * This creates a HttpMethod with the message as its payload and image attachment. The message
   * should be a properly formatted JSON String (No validation is done on this).
   *
   * <p>The message can be easily created using the {@link #getJsonPayload(Message)} method.
   *
   * @param uri The full URI which we will post to
   * @param message A properly formatted JSON message. UTF-8 is expected
   * @param image A complete instance of ImageAttachment object
   * @throws IOException
   */
  public HttpMethod getJsonPostForMultipartRequestEntity(
      String uri, String message, ImageAttachment image) throws IOException {
    PostMethod post = new PostMethod(uri);

    StringPart bodyPart = new StringPart("json", message);
    bodyPart.setContentType("application/json");

    FilePart filePart = new FilePart("feedItemFileUpload", image.retrieveObjectFile());
    filePart.setContentType(image.retrieveContentType());

    Part[] parts = {
      bodyPart, filePart,
    };

    post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));

    return post;
  }
Esempio n. 4
0
  public Response multPartURL(
      String fileParamName,
      String url,
      PostParameter[] params,
      File file,
      boolean authenticated,
      String token)
      throws WeiboException {
    PostMethod postMethod = new PostMethod(url);
    try {
      Part[] parts = null;
      if (params == null) {
        parts = new Part[1];
      } else {
        parts = new Part[params.length + 1];
      }
      if (params != null) {
        int i = 0;
        for (PostParameter entry : params) {
          parts[i++] = new StringPart(entry.getName(), (String) entry.getValue());
        }
      }
      FilePart filePart =
          new FilePart(
              fileParamName,
              file.getName(),
              file,
              new MimetypesFileTypeMap().getContentType(file),
              "UTF-8");
      filePart.setTransferEncoding("binary");
      parts[parts.length - 1] = filePart;

      postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
      return httpRequest(postMethod, token);
    } catch (Exception ex) {
      throw new WeiboException(ex.getMessage(), ex, -1);
    }
  }
Esempio n. 5
0
  public Response multPartURL(
      String fileParamName, String url, PostParameter[] params, File file, boolean authenticated)
      throws WeiboException {
    PostMethod post = new PostMethod(url);
    org.apache.commons.httpclient.HttpClient client = getHttpClient();
    try {
      long t = System.currentTimeMillis();
      Part[] parts = null;
      if (params == null) {
        parts = new Part[1];
      } else {
        parts = new Part[params.length + 1];
      }
      if (params != null) {
        int i = 0;
        for (PostParameter entry : params) {
          parts[i++] = new StringPart(entry.getName(), (String) entry.getValue());
        }
      }
      FilePart filePart =
          new FilePart(
              fileParamName,
              file.getName(),
              file,
              new MimetypesFileTypeMap().getContentType(file),
              "UTF-8");
      filePart.setTransferEncoding("binary");
      parts[parts.length - 1] = filePart;

      post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
      List<Header> headers = new ArrayList<Header>();

      if (authenticated) {
        if (oauth == null) {}
        String authorization = null;
        if (null != oauth) {
          // use OAuth
          authorization = oauth.generateAuthorizationHeader("POST", url, params, oauthToken);
        } else {
          throw new IllegalStateException(
              "Neither user ID/password combination nor OAuth consumer key/secret combination supplied");
        }
        headers.add(new Header("Authorization", authorization));
        log("Authorization: " + authorization);
      }
      client.getHostConfiguration().getParams().setParameter("http.default-headers", headers);
      client.executeMethod(post);

      Response response = new Response();
      response.setResponseAsString(post.getResponseBodyAsString());
      response.setStatusCode(post.getStatusCode());

      log(
          "multPartURL URL:"
              + url
              + ", result:"
              + response
              + ", time:"
              + (System.currentTimeMillis() - t));
      return response;
    } catch (Exception ex) {
      throw new WeiboException(ex.getMessage(), ex, -1);
    } finally {
      post.releaseConnection();
      client = null;
    }
  }