Example #1
0
  public Response executeTask() throws Exception {
    if (lite.isCacheAble(this)) lite.addCacheHeaders(request);
    String urlStr = request.getUrl();
    URL url = new URL(urlStr);
    HttpURLConnection connection;
    if (lite.settings.getProxy() != null) {
      connection = (HttpURLConnection) url.openConnection(lite.settings.getProxy());
    } else {
      connection = (HttpURLConnection) url.openConnection();
    }
    assertCanceled();
    connection.setReadTimeout(lite.settings.getReadTimeout());
    connection.setConnectTimeout(lite.settings.getConnectTimeout());
    connection.setInstanceFollowRedirects(lite.settings.isFollowRedirects());
    if (connection instanceof HttpsURLConnection) {
      HttpsURLConnection httpsURLConnection = (HttpsURLConnection) connection;
      httpsURLConnection.setSSLSocketFactory(lite.settings.getSslSocketFactory());
      httpsURLConnection.setHostnameVerifier(lite.settings.getHostnameVerifier());
    }
    lite.processCookie(urlStr, request.getHeaders());
    if (request.getHeaders() != null && !request.getHeaders().isEmpty()) {
      boolean first;
      for (String name : request.getHeaders().keySet()) {
        first = true;
        for (String value : request.getHeaders().get(name)) {
          if (first) {
            connection.setRequestProperty(name, value);
            first = false;
          } else {
            connection.addRequestProperty(name, value);
          }
        }
      }
    }
    connection.setRequestMethod(request.getMethod().name());

    connection.setDoInput(true);
    if (request.getMethod().permitsRequestBody && request.getBody() != null) {
      connection.setRequestProperty("Content-Type", request.getBody().contentType().toString());
      long contentLength = request.getBody().contentLength();
      if (contentLength < 0) {
        connection.setChunkedStreamingMode(256 * 1024);
      } else {
        if (contentLength < Integer.MAX_VALUE) {
          connection.setFixedLengthStreamingMode((int) contentLength);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
          connection.setFixedLengthStreamingMode(contentLength);
        } else {
          connection.setChunkedStreamingMode(256 * 1024);
        }
      }
      connection.setRequestProperty("Content-Length", String.valueOf(contentLength));
      connection.setDoOutput(true);
      request.getBody().writeTo(connection.getOutputStream());
    }

    Response response = URLite.createResponse(connection, request);
    lite.saveCookie(urlStr, response.headers());
    isExecuted = true;
    if (!lite.isCacheAble(this)) {
      return response;
    } else {
      return lite.createCacheResponse(response);
    }
  }
Example #2
0
 public Object tag() {
   return request.getTag();
 }