Esempio n. 1
0
  public Response multPartURL(
      String url, PostParameter[] params, ImageItem item, boolean authenticated)
      throws WeiboException {
    PostMethod post = new PostMethod(url);
    try {
      org.apache.commons.httpclient.HttpClient client = getHttpClient();
      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());
        }
        parts[parts.length - 1] =
            new ByteArrayPart(item.getContent(), item.getName(), item.getContentType());
      }
      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();
    }
  }
Esempio n. 2
0
 /** 处理http deletemethod请求 */
 public static String delete(String url, PostParameter[] params, String token) throws IOException {
   if (0 != params.length) {
     String encodedParams = PostParameter.encodeParameters(params);
     if (-1 == url.indexOf("?")) {
       url += "?" + encodedParams;
     } else {
       url += "&" + encodedParams;
     }
   }
   DeleteMethod deleteMethod = new DeleteMethod(url);
   return httpRequest(deleteMethod, token);
 }
Esempio n. 3
0
 /**
  * get
  *
  * @param url
  * @param params
  * @return
  * @throws ApiException
  */
 public static String get(String url, PostParameter[] params, String token) throws IOException {
   if (null != params && params.length > 0) {
     String encodedParams = PostParameter.encodeParameters(params);
     if (-1 == url.indexOf("?")) {
       url += "?" + encodedParams;
     } else {
       url += "&" + encodedParams;
     }
   }
   GetMethod getmethod = new GetMethod(url);
   return httpRequest(getmethod, token);
 }
Esempio n. 4
0
 /**
  * 支持multipart方式上传
  *
  * @param url
  * @param params
  * @param token
  * @return
  * @throws ApiException
  */
 public static String multPartURL(String url, PostParameter[] params, String token)
     throws IOException {
   PostMethod postMethod = new PostMethod(url);
   if (params != null) {
     Part[] parts = new Part[params.length];
     int i = 0;
     for (PostParameter entry : params) {
       if (entry.isFile()) {
         parts[i++] =
             new ByteArrayPart(
                 entry.getFile().getContent(), entry.getName(), entry.getFile().getMimeType());
       } else {
         parts[i++] = new StringPart(entry.getName(), entry.getValue(), DEFAULTCHAESET);
       }
     }
     postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
   }
   return httpRequest(postMethod, token);
 }