okhttp3.Request.url()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(7.5k)|赞(0)|评价(0)|浏览(340)

本文整理了Java中okhttp3.Request.url方法的一些代码示例,展示了Request.url的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Request.url方法的具体详情如下:
包路径:okhttp3.Request
类名称:Request
方法名:url

Request.url介绍

暂无

代码示例

代码示例来源:origin: square/okhttp

@Override public String toString() {
 return "Response{protocol="
   + protocol
   + ", code="
   + code
   + ", message="
   + message
   + ", url="
   + request.url()
   + '}';
}

代码示例来源:origin: square/okhttp

@Override public Response intercept(Chain chain) throws IOException {
  long t1 = System.nanoTime();
  Request request = chain.request();
  logger.info(String.format("Sending request %s on %s%n%s",
    request.url(), chain.connection(), request.headers()));
  Response response = chain.proceed(request);
  long t2 = System.nanoTime();
  logger.info(String.format("Received response for %s in %.1fms%n%s",
    request.url(), (t2 - t1) / 1e6d, response.headers()));
  return response;
 }
}

代码示例来源:origin: square/okhttp

public boolean matches(Request request, Response response) {
 return url.equals(request.url().toString())
   && requestMethod.equals(request.method())
   && HttpHeaders.varyMatches(response, varyHeaders, request);
}

代码示例来源:origin: square/okhttp

@Override public Response intercept(Chain chain) throws IOException {
  Request request = chain.request();
  if (request.url().host().equals(host)) {
   request = request.newBuilder()
     .header("Authorization", credentials)
     .build();
  }
  return chain.proceed(request);
 }
}

代码示例来源:origin: square/retrofit

@Override public void onFailure(Call<Page> call, Throwable t) {
  System.out.println(call.request().url() + ": failed: " + t);
 }
});

代码示例来源:origin: square/okhttp

String host() {
 return originalRequest.url().host();
}

代码示例来源:origin: square/okhttp

String redactedUrl() {
 return originalRequest.url().redact();
}

代码示例来源:origin: square/okhttp

/**
 * Returns the request status line, like "GET / HTTP/1.1". This is exposed to the application by
 * {@link HttpURLConnection#getHeaderFields}, so it needs to be set even if the transport is
 * HTTP/2.
 */
public static String get(Request request, Proxy.Type proxyType) {
 StringBuilder result = new StringBuilder();
 result.append(request.method());
 result.append(' ');
 if (includeAuthorityInRequestLine(request, proxyType)) {
  result.append(request.url());
 } else {
  result.append(requestPath(request.url()));
 }
 result.append(" HTTP/1.1");
 return result.toString();
}

代码示例来源:origin: square/okhttp

CacheHttpURLConnection(Response response) {
 super(response.request().url().url());
 this.request = response.request();
 this.response = response;
 // Configure URLConnection inherited fields.
 this.connected = true;
 this.doOutput = request.body() != null;
 this.doInput = true;
 this.useCaches = true;
 // Configure HttpUrlConnection inherited fields.
 this.method = request.method();
}

代码示例来源:origin: square/okhttp

@Override public EventListener create(Call call) {
  long callId = nextCallId.getAndIncrement();
  System.out.printf("%04d %s%n", callId, call.request().url());
  return new PrintingEventListener(callId, System.nanoTime());
 }
};

代码示例来源:origin: square/retrofit

@Override public okhttp3.Response intercept(Chain chain) throws IOException {
  Request request = chain.request();
  String host = this.host;
  if (host != null) {
   HttpUrl newUrl = request.url().newBuilder()
     .host(host)
     .build();
   request = request.newBuilder()
     .url(newUrl)
     .build();
  }
  return chain.proceed(request);
 }
}

代码示例来源:origin: square/okhttp

@Override public void onResponse(Call call, Response response) {
 synchronized (lock) {
  this.response = response;
  this.handshake = response.handshake();
  this.url = response.request().url().url();
  lock.notifyAll();
 }
}

代码示例来源:origin: square/okhttp

Entry(Response response) {
 this.url = response.request().url().toString();
 this.varyHeaders = HttpHeaders.varyHeaders(response);
 this.requestMethod = response.request().method();
 this.protocol = response.protocol();
 this.code = response.code();
 this.message = response.message();
 this.responseHeaders = response.headers();
 this.handshake = response.handshake();
 this.sentRequestMillis = response.sentRequestAtMillis();
 this.receivedResponseMillis = response.receivedResponseAtMillis();
}

代码示例来源:origin: square/retrofit

public static void main(String... args) throws IOException {
  HostSelectionInterceptor hostSelectionInterceptor = new HostSelectionInterceptor();

  OkHttpClient okHttpClient = new OkHttpClient.Builder()
    .addInterceptor(hostSelectionInterceptor)
    .build();

  Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("http://www.github.com/")
    .callFactory(okHttpClient)
    .build();

  Pop pop = retrofit.create(Pop.class);

  Response<ResponseBody> response1 = pop.robots().execute();
  System.out.println("Response from: " + response1.raw().request().url());
  System.out.println(response1.body().string());

  hostSelectionInterceptor.setHost("www.pepsi.com");

  Response<ResponseBody> response2 = pop.robots().execute();
  System.out.println("Response from: " + response2.raw().request().url());
  System.out.println(response2.body().string());
 }
}

代码示例来源:origin: square/okhttp

void remove(Request request) throws IOException {
 cache.remove(key(request.url()));
}

代码示例来源:origin: square/okhttp

/**
  * Returns the {@link CacheResponse} from the delegate by converting the OkHttp {@link Request}
  * into the arguments required by the {@link ResponseCache}.
  */
 private CacheResponse getJavaCachedResponse(Request request) throws IOException {
  Map<String, List<String>> headers = JavaApiConverter.extractJavaHeaders(request);
  return delegate.get(request.url().uri(), request.method(), headers);
 }
}

代码示例来源:origin: square/retrofit

@Override public void onResponse(Call<Page> call, Response<Page> response) {
 if (!response.isSuccessful()) {
  System.out.println(call.request().url() + ": failed: " + response.code());
  return;
 }
 // Print this page's URL and title.
 Page page = response.body();
 HttpUrl base = response.raw().request().url();
 System.out.println(base + ": " + page.title);
 // Enqueue its links for visiting.
 for (String link : page.links) {
  HttpUrl linkUrl = base.resolve(link);
  if (linkUrl != null && fetchedUrls.add(linkUrl)) {
   crawlPage(linkUrl);
  }
 }
}

代码示例来源:origin: square/okhttp

@Override public CacheRequest put(Response response) throws IOException {
 URI uri = response.request().url().uri();
 HttpURLConnection connection = JavaApiConverter.createJavaUrlConnectionForCachePut(response);
 final java.net.CacheRequest request = delegate.put(uri, connection);
 if (request == null) {
  return null;
 }
 return new CacheRequest() {
  @Override public Sink body() throws IOException {
   OutputStream body = request.getBody();
   return body != null ? Okio.sink(body) : null;
  }
  @Override public void abort() {
   request.abort();
  }
 };
}

代码示例来源:origin: square/okhttp

/**
  * Returns true if an HTTP request for {@code followUp} can reuse the connection used by this
  * engine.
  */
 private boolean sameConnection(Response response, HttpUrl followUp) {
  HttpUrl url = response.request().url();
  return url.host().equals(followUp.host())
    && url.port() == followUp.port()
    && url.scheme().equals(followUp.scheme());
 }
}

代码示例来源:origin: square/okhttp

@Override public ResponseBody openResponseBody(Response response) throws IOException {
 streamAllocation.eventListener.responseBodyStart(streamAllocation.call);
 String contentType = response.header("Content-Type");
 if (!HttpHeaders.hasBody(response)) {
  Source source = newFixedLengthSource(0);
  return new RealResponseBody(contentType, 0, Okio.buffer(source));
 }
 if ("chunked".equalsIgnoreCase(response.header("Transfer-Encoding"))) {
  Source source = newChunkedSource(response.request().url());
  return new RealResponseBody(contentType, -1L, Okio.buffer(source));
 }
 long contentLength = HttpHeaders.contentLength(response);
 if (contentLength != -1) {
  Source source = newFixedLengthSource(contentLength);
  return new RealResponseBody(contentType, contentLength, Okio.buffer(source));
 }
 return new RealResponseBody(contentType, -1L, Okio.buffer(newUnknownLengthSource()));
}

相关文章