Beispiel #1
0
 private Request networkRequest(Request request)
 {
     com.squareup.okhttp.Request.Builder builder = request.newBuilder();
     if (request.header("Host") == null)
     {
         builder.header("Host", Util.hostHeader(request.httpUrl()));
     }
     if ((connection == null || connection.getProtocol() != Protocol.HTTP_1_0) && request.header("Connection") == null)
     {
         builder.header("Connection", "Keep-Alive");
     }
     if (request.header("Accept-Encoding") == null)
     {
         transparentGzip = true;
         builder.header("Accept-Encoding", "gzip");
     }
     CookieHandler cookiehandler = client.getCookieHandler();
     if (cookiehandler != null)
     {
         java.util.Map map = OkHeaders.toMultimap(builder.build().headers(), null);
         OkHeaders.addCookies(builder, cookiehandler.get(request.uri(), map));
     }
     if (request.header("User-Agent") == null)
     {
         builder.header("User-Agent", Version.userAgent());
     }
     return builder.build();
 }
 @Override
 public Map<String, String> getAllHeaders() {
   HashMap<String, String> headers = new HashMap<>();
   for (String key : request.headers().names()) {
     headers.put(key, request.header(key));
   }
   return headers;
 }
Beispiel #3
0
 @Test
 public void userAgent() {
   Request request = fromArgs("-A", "foo", "http://example.com").createRequest();
   assertEquals("GET", request.method());
   assertEquals("http://example.com", request.urlString());
   assertEquals("foo", request.header("User-Agent"));
   assertNull(request.body());
 }
Beispiel #4
0
 @Test
 public void referer() {
   Request request = fromArgs("-e", "foo", "http://example.com").createRequest();
   assertEquals("GET", request.method());
   assertEquals("http://example.com", request.urlString());
   assertEquals("foo", request.header("Referer"));
   assertNull(request.body());
 }
  @Override
  public Response intercept(Chain chain) throws IOException {
    Request originalRequest = chain.request();
    if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {
      return chain.proceed(originalRequest);
    }

    Request compressedRequest =
        originalRequest
            .newBuilder()
            .header("Content-Encoding", "gzip")
            .method(originalRequest.method(), gzip(originalRequest.body()))
            .build();
    return chain.proceed(compressedRequest);
  }
  @Override
  public Sink createRequestBody(Request request, long contentLength) throws IOException {
    if ("chunked".equalsIgnoreCase(request.header("Transfer-Encoding"))) {
      // Stream a request body of unknown length.
      return httpConnection.newChunkedSink();
    }

    if (contentLength != -1) {
      // Stream a request body of a known length.
      return httpConnection.newFixedLengthSink(contentLength);
    }

    throw new IllegalStateException(
        "Cannot stream a request body without chunked encoding or a known content length!");
  }
 @Override
 public Response intercept(Chain chain) throws IOException {
   Request originalRequest = chain.request();
   Request.Builder request = originalRequest.newBuilder();
   if (originalRequest.header("fresh") != null) {
     request.cacheControl(CacheControl.FORCE_NETWORK);
   }
   Response response = chain.proceed(request.build());
   return response
       .newBuilder()
       .removeHeader("Pragma")
       .removeHeader("Cache-Control")
       .header("Cache-Control", cacheControl())
       .build();
 }
Beispiel #8
0
 /**
  * @param context
  * @param bundle
  */
 public AbstractBaseOkHttp(@NonNull Context context, @NonNull Bundle bundle) {
   this.context = context;
   this.bundle = bundle;
   printLog = new PrintLog();
   printLog.setRequestTime(
       TimeUtils.getCurrentTimeInString(new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒")));
   request = getRequest();
   printLog.setUrl(request.urlString());
   printLog.setHeaders(
       new StringBuilder()
           .append(CONTENT_TYPE_KEY)
           .append("=")
           .append(request.header(CONTENT_TYPE_KEY))
           .toString());
   execute(getOkHttpClient(), getRequest(), getResponseCallBack());
 }
 @Override
 public Response intercept(Chain chain) throws IOException {
   Request request = chain.request();
   Optional<String> appAuthHeader = Optional.fromNullable(request.header(X_API_KEY_HEADER));
   if (appAuthHeader.isPresent()) {
     final Request.Builder newRequest = request.newBuilder();
     final String today = getTodayDate();
     newRequest.addHeader(DATE_HEADER, today);
     Signature signature =
         getSigner()
             .sign(
                 request.method(),
                 request.urlString(),
                 Collections.singletonMap(DATE_HEADER, today));
     newRequest.addHeader(AUTHORIZATION_HEADER, signature.toString());
     request = newRequest.build();
   }
   return chain.proceed(request);
 }
 @Override
 public String getHeader(String key) {
   return request.header(key);
 }
Beispiel #11
0
 @Nullable
 @Override
 public String firstHeaderValue(String name) {
   return mRequest.header(name);
 }
Beispiel #12
0
 /**
  * Returns true if the request contains conditions that save the server from sending a response
  * that the client has locally. When a request is enqueued with its own conditions, the built-in
  * response cache won't be used.
  */
 private static boolean hasConditions(Request request) {
   return request.header("If-Modified-Since") != null || request.header("If-None-Match") != null;
 }