示例#1
0
  /**
   * sets HTTP headers
   *
   * @param connection HttpURLConnection
   * @param authenticated boolean
   */
  private void setHeaders(
      String url,
      PostParameter[] params,
      HttpURLConnection connection,
      boolean authenticated,
      String httpMethod) {
    log("Request: ");
    log(httpMethod + " ", url);

    if (authenticated) {
      if (oauth == null) {}
      String authorization = null;
      if (null != oauth) {
        // use OAuth
        authorization = oauth.generateAuthorizationHeader(httpMethod, url, params, oauthToken);
      } else {
        throw new IllegalStateException(
            "Neither user ID/password combination nor OAuth consumer key/secret combination supplied");
      }
      connection.addRequestProperty("Authorization", authorization);
      log("Authorization: " + authorization);
    }
    for (String key : requestHeaders.keySet()) {
      connection.addRequestProperty(key, requestHeaders.get(key));
      log(key + ": " + requestHeaders.get(key));
    }
  }
示例#2
0
  public HttpClient(int maxConPerHost, int conTimeOutMs, int soTimeOutMs, int maxSize) {
    connectionManager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams params = connectionManager.getParams();
    params.setDefaultMaxConnectionsPerHost(maxConPerHost);
    params.setConnectionTimeout(conTimeOutMs);
    params.setSoTimeout(soTimeOutMs);

    HttpClientParams clientParams = new HttpClientParams();
    // 忽略cookie 避免 Cookie rejected 警告
    clientParams.setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
    client = new org.apache.commons.httpclient.HttpClient(clientParams, connectionManager);
    Protocol myhttps = new Protocol("https", new MySSLSocketFactory(), 443);
    Protocol.registerProtocol("https", myhttps);
    this.maxSize = maxSize;
    // 支持proxy
    if (proxyHost != null && !proxyHost.equals("")) {
      client.getHostConfiguration().setProxy(proxyHost, proxyPort);
      client.getParams().setAuthenticationPreemptive(true);
      if (proxyAuthUser != null && !proxyAuthUser.equals("")) {
        client
            .getState()
            .setProxyCredentials(
                AuthScope.ANY, new UsernamePasswordCredentials(proxyAuthUser, proxyAuthPassword));
        log("Proxy AuthUser: "******"Proxy AuthPassword: " + proxyAuthPassword);
      }
    }
  }
示例#3
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();
    }
  }
示例#4
0
 public Response post(String url, PostParameter[] params, Boolean WithTokenHeader, String token)
     throws WeiboException {
   log("Request:");
   log("POST" + url);
   PostMethod postMethod = new PostMethod(url);
   for (int i = 0; i < params.length; i++) {
     postMethod.addParameter(params[i].getName(), params[i].getValue());
   }
   HttpMethodParams param = postMethod.getParams();
   param.setContentCharset("UTF-8");
   return httpRequest(postMethod, WithTokenHeader, token);
 }
示例#5
0
  public Response httpRequest(HttpMethod method, Boolean WithTokenHeader, String token)
      throws WeiboException {
    InetAddress ipaddr;
    int responseCode = -1;
    try {
      ipaddr = InetAddress.getLocalHost();
      List<Header> headers = new ArrayList<Header>();
      if (WithTokenHeader) {
        if (token == null) {
          throw new IllegalStateException("Oauth2 token is not set!");
        }
        headers.add(new Header("Authorization", "OAuth2 " + token));
        headers.add(new Header("API-RemoteIP", ipaddr.getHostAddress()));
        client.getHostConfiguration().getParams().setParameter("http.default-headers", headers);
        for (Header hd : headers) {
          log(hd.getName() + ": " + hd.getValue());
        }
      }

      method
          .getParams()
          .setParameter(
              HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
      client.executeMethod(method);
      Header[] resHeader = method.getResponseHeaders();
      responseCode = method.getStatusCode();
      log("Response:");
      log("https StatusCode:" + String.valueOf(responseCode));

      for (Header header : resHeader) {
        log(header.getName() + ":" + header.getValue());
      }
      Response response = new Response();
      response.setResponseAsString(method.getResponseBodyAsString());
      log(response.toString() + "\n");

      if (responseCode != OK) {
        try {
          throw new WeiboException(
              getCause(responseCode), response.asJSONObject(), method.getStatusCode());
        } catch (JSONException e) {
          e.printStackTrace();
        }
      }
      return response;

    } catch (IOException ioe) {
      throw new WeiboException(ioe.getMessage(), ioe, responseCode);
    } finally {
      method.releaseConnection();
    }
  }
示例#6
0
 public Response get(String url, PostParameter[] params, String token) throws WeiboException {
   log("Request:");
   log("GET:" + url);
   if (null != params && params.length > 0) {
     String encodedParams = HttpClient.encodeParameters(params);
     if (-1 == url.indexOf("?")) {
       url += "?" + encodedParams;
     } else {
       url += "&" + encodedParams;
     }
   }
   GetMethod getmethod = new GetMethod(url);
   return httpRequest(getmethod, token);
 }
示例#7
0
 private org.apache.commons.httpclient.HttpClient getHttpClient() {
   org.apache.commons.httpclient.HttpClient client =
       new org.apache.commons.httpclient.HttpClient();
   if (proxyHost != null && !proxyHost.equals("")) {
     client.getHostConfiguration().setProxy(proxyHost, proxyPort);
     client.getParams().setAuthenticationPreemptive(true);
     if (proxyAuthUser != null && !proxyAuthUser.equals("")) {
       client
           .getState()
           .setProxyCredentials(
               AuthScope.ANY, new UsernamePasswordCredentials(proxyAuthUser, proxyAuthPassword));
       log("Proxy AuthUser: "******"Proxy AuthPassword: " + proxyAuthPassword);
     }
   }
   return client;
 }
示例#8
0
 private HttpURLConnection getConnection(String url) throws IOException {
   HttpURLConnection con = null;
   if (proxyHost != null && !proxyHost.equals("")) {
     if (proxyAuthUser != null && !proxyAuthUser.equals("")) {
       log("Proxy AuthUser: "******"Proxy AuthPassword: "******"Opening proxied connection(" + proxyHost + ":" + proxyPort + ")");
     }
     con = (HttpURLConnection) new URL(url).openConnection(proxy);
   } else {
     con = (HttpURLConnection) new URL(url).openConnection();
   }
   if (connectionTimeout > 0 && !isJDK14orEarlier) {
     con.setConnectTimeout(connectionTimeout);
   }
   if (readTimeout > 0 && !isJDK14orEarlier) {
     con.setReadTimeout(readTimeout);
   }
   return con;
 }
示例#9
0
 private static void log(String message, String message2) {
   if (DEBUG) {
     log(message + message2);
   }
 }
示例#10
0
  public Response httpRequest(
      String url, PostParameter[] postParams, boolean authenticated, String httpMethod)
      throws WeiboException {
    int retriedCount;
    int retry = retryCount + 1;
    Response res = null;
    for (retriedCount = 0; retriedCount < retry; retriedCount++) {
      int responseCode = -1;
      try {
        HttpURLConnection con = null;
        OutputStream osw = null;
        try {
          con = getConnection(url);
          con.setDoInput(true);
          setHeaders(url, postParams, con, authenticated, httpMethod);
          if (null != postParams || "POST".equals(httpMethod)) {
            con.setRequestMethod("POST");
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            con.setDoOutput(true);
            String postParam = "";
            if (postParams != null) {
              postParam = encodeParameters(postParams);
            }
            log("Post Params: ", postParam);
            byte[] bytes = postParam.getBytes("UTF-8");

            con.setRequestProperty("Content-Length", Integer.toString(bytes.length));

            osw = con.getOutputStream();
            osw.write(bytes);
            osw.flush();
            osw.close();
          } else if ("DELETE".equals(httpMethod)) {
            con.setRequestMethod("DELETE");
          } else {
            con.setRequestMethod("GET");
          }

          res = new Response(con);
          responseCode = con.getResponseCode();
          if (DEBUG) {
            log("Response: ");
            Map<String, List<String>> responseHeaders = con.getHeaderFields();
            for (String key : responseHeaders.keySet()) {
              List<String> values = responseHeaders.get(key);
              for (String value : values) {
                if (null != key) {
                  log(key + ": " + value);
                } else {
                  log(value);
                }
              }
            }
          }
          if (responseCode != OK) {
            if (responseCode < INTERNAL_SERVER_ERROR || retriedCount == retryCount) {
              throw new WeiboException(
                  getCause(responseCode) + "\n" + res.asString(), responseCode);
            }
            // will retry if the status code is INTERNAL_SERVER_ERROR
          } else {
            break;
          }
        } finally {
          try {
            osw.close();
          } catch (Exception ignore) {
          }
        }
      } catch (IOException ioe) {
        // connection timeout or read timeout
        if (retriedCount == retryCount) {
          throw new WeiboException(ioe.getMessage(), ioe, responseCode);
        }
      }
      try {
        if (DEBUG && null != res) {
          res.asString();
        }
        log("Sleeping " + retryIntervalMillis + " millisecs for next retry.");
        Thread.sleep(retryIntervalMillis);
      } catch (InterruptedException ignore) {
        // nothing to do
      }
    }
    return res;
  }