Пример #1
0
 public Map<String, List<String>> getCookies(URL url) {
   try {
     return cookieManager.get(url.toURI(), new HashMap<String, List<String>>());
   } catch (IOException e) {
     throw new HttpToolsException("Error on cookie storing: " + url, e);
   } catch (URISyntaxException e) {
     throw new HttpToolsException("Url syntax error on cookie storing: " + url, e);
   }
 }
Пример #2
0
  /**
   * invoke via Loader
   *
   * @throws IOException
   */
  @Override
  @TargetApi(Build.VERSION_CODES.KITKAT)
  public void sendRequest() throws IOException {
    isLoading = false;

    URL url = new URL(queryUrl);
    { // init connection
      Proxy proxy = params.getProxy();
      if (proxy != null) {
        connection = (HttpURLConnection) url.openConnection(proxy);
      } else {
        connection = (HttpURLConnection) url.openConnection();
      }
      connection.setInstanceFollowRedirects(true);
      connection.setReadTimeout(params.getConnectTimeout());
      connection.setConnectTimeout(params.getConnectTimeout());
      if (connection instanceof HttpsURLConnection) {
        ((HttpsURLConnection) connection).setSSLSocketFactory(params.getSslSocketFactory());
      }
    }

    { // add headers
      try {
        Map<String, List<String>> singleMap =
            COOKIE_MANAGER.get(url.toURI(), new HashMap<String, List<String>>(0));
        List<String> cookies = singleMap.get("Cookie");
        if (cookies != null) {
          connection.setRequestProperty("Cookie", TextUtils.join(";", cookies));
        }
      } catch (Throwable ex) {
        LogUtil.e(ex.getMessage(), ex);
      }

      HashMap<String, String> headers = params.getHeaders();
      if (headers != null) {
        for (Map.Entry<String, String> entry : headers.entrySet()) {
          String name = entry.getKey();
          String value = entry.getValue();
          if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(value)) {
            connection.setRequestProperty(name, value);
          }
        }
      }
    }

    { // write body
      HttpMethod method = params.getMethod();
      connection.setRequestMethod(method.toString());
      if (HttpMethod.permitsRequestBody(method)) {
        RequestBody body = params.getRequestBody();
        if (body != null) {
          if (body instanceof ProgressBody) {
            ((ProgressBody) body).setProgressHandler(progressHandler);
          }
          String contentType = body.getContentType();
          if (!TextUtils.isEmpty(contentType)) {
            connection.setRequestProperty("Content-Type", contentType);
          }
          long contentLength = body.getContentLength();
          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);
          body.writeTo(connection.getOutputStream());
        }
      }
    }

    LogUtil.d(queryUrl);
    int code = connection.getResponseCode();
    if (code >= 300) {
      HttpException httpException = new HttpException(code, connection.getResponseMessage());
      try {
        httpException.setResult(IOUtil.readStr(connection.getInputStream(), params.getCharset()));
      } catch (Throwable ignored) {
      }
      throw httpException;
    }

    { // save cookies
      try {
        Map<String, List<String>> headers = connection.getHeaderFields();
        if (headers != null) {
          COOKIE_MANAGER.put(url.toURI(), headers);
        }
      } catch (Throwable ex) {
        LogUtil.e(ex.getMessage(), ex);
      }
    }

    isLoading = true;
  }