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

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

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

Request.url介绍

暂无

代码示例

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

  1. @Override public String toString() {
  2. return "Response{protocol="
  3. + protocol
  4. + ", code="
  5. + code
  6. + ", message="
  7. + message
  8. + ", url="
  9. + request.url()
  10. + '}';
  11. }

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

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

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

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

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

  1. @Override public Response intercept(Chain chain) throws IOException {
  2. Request request = chain.request();
  3. if (request.url().host().equals(host)) {
  4. request = request.newBuilder()
  5. .header("Authorization", credentials)
  6. .build();
  7. }
  8. return chain.proceed(request);
  9. }
  10. }

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

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

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

  1. String host() {
  2. return originalRequest.url().host();
  3. }

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

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

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

  1. /**
  2. * Returns the request status line, like "GET / HTTP/1.1". This is exposed to the application by
  3. * {@link HttpURLConnection#getHeaderFields}, so it needs to be set even if the transport is
  4. * HTTP/2.
  5. */
  6. public static String get(Request request, Proxy.Type proxyType) {
  7. StringBuilder result = new StringBuilder();
  8. result.append(request.method());
  9. result.append(' ');
  10. if (includeAuthorityInRequestLine(request, proxyType)) {
  11. result.append(request.url());
  12. } else {
  13. result.append(requestPath(request.url()));
  14. }
  15. result.append(" HTTP/1.1");
  16. return result.toString();
  17. }

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

  1. CacheHttpURLConnection(Response response) {
  2. super(response.request().url().url());
  3. this.request = response.request();
  4. this.response = response;
  5. // Configure URLConnection inherited fields.
  6. this.connected = true;
  7. this.doOutput = request.body() != null;
  8. this.doInput = true;
  9. this.useCaches = true;
  10. // Configure HttpUrlConnection inherited fields.
  11. this.method = request.method();
  12. }

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

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

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

  1. @Override public okhttp3.Response intercept(Chain chain) throws IOException {
  2. Request request = chain.request();
  3. String host = this.host;
  4. if (host != null) {
  5. HttpUrl newUrl = request.url().newBuilder()
  6. .host(host)
  7. .build();
  8. request = request.newBuilder()
  9. .url(newUrl)
  10. .build();
  11. }
  12. return chain.proceed(request);
  13. }
  14. }

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

  1. @Override public void onResponse(Call call, Response response) {
  2. synchronized (lock) {
  3. this.response = response;
  4. this.handshake = response.handshake();
  5. this.url = response.request().url().url();
  6. lock.notifyAll();
  7. }
  8. }

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

  1. Entry(Response response) {
  2. this.url = response.request().url().toString();
  3. this.varyHeaders = HttpHeaders.varyHeaders(response);
  4. this.requestMethod = response.request().method();
  5. this.protocol = response.protocol();
  6. this.code = response.code();
  7. this.message = response.message();
  8. this.responseHeaders = response.headers();
  9. this.handshake = response.handshake();
  10. this.sentRequestMillis = response.sentRequestAtMillis();
  11. this.receivedResponseMillis = response.receivedResponseAtMillis();
  12. }

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

  1. public static void main(String... args) throws IOException {
  2. HostSelectionInterceptor hostSelectionInterceptor = new HostSelectionInterceptor();
  3. OkHttpClient okHttpClient = new OkHttpClient.Builder()
  4. .addInterceptor(hostSelectionInterceptor)
  5. .build();
  6. Retrofit retrofit = new Retrofit.Builder()
  7. .baseUrl("http://www.github.com/")
  8. .callFactory(okHttpClient)
  9. .build();
  10. Pop pop = retrofit.create(Pop.class);
  11. Response<ResponseBody> response1 = pop.robots().execute();
  12. System.out.println("Response from: " + response1.raw().request().url());
  13. System.out.println(response1.body().string());
  14. hostSelectionInterceptor.setHost("www.pepsi.com");
  15. Response<ResponseBody> response2 = pop.robots().execute();
  16. System.out.println("Response from: " + response2.raw().request().url());
  17. System.out.println(response2.body().string());
  18. }
  19. }

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

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

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

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

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

  1. @Override public void onResponse(Call<Page> call, Response<Page> response) {
  2. if (!response.isSuccessful()) {
  3. System.out.println(call.request().url() + ": failed: " + response.code());
  4. return;
  5. }
  6. // Print this page's URL and title.
  7. Page page = response.body();
  8. HttpUrl base = response.raw().request().url();
  9. System.out.println(base + ": " + page.title);
  10. // Enqueue its links for visiting.
  11. for (String link : page.links) {
  12. HttpUrl linkUrl = base.resolve(link);
  13. if (linkUrl != null && fetchedUrls.add(linkUrl)) {
  14. crawlPage(linkUrl);
  15. }
  16. }
  17. }

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

  1. @Override public CacheRequest put(Response response) throws IOException {
  2. URI uri = response.request().url().uri();
  3. HttpURLConnection connection = JavaApiConverter.createJavaUrlConnectionForCachePut(response);
  4. final java.net.CacheRequest request = delegate.put(uri, connection);
  5. if (request == null) {
  6. return null;
  7. }
  8. return new CacheRequest() {
  9. @Override public Sink body() throws IOException {
  10. OutputStream body = request.getBody();
  11. return body != null ? Okio.sink(body) : null;
  12. }
  13. @Override public void abort() {
  14. request.abort();
  15. }
  16. };
  17. }

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

  1. /**
  2. * Returns true if an HTTP request for {@code followUp} can reuse the connection used by this
  3. * engine.
  4. */
  5. private boolean sameConnection(Response response, HttpUrl followUp) {
  6. HttpUrl url = response.request().url();
  7. return url.host().equals(followUp.host())
  8. && url.port() == followUp.port()
  9. && url.scheme().equals(followUp.scheme());
  10. }
  11. }

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

  1. @Override public ResponseBody openResponseBody(Response response) throws IOException {
  2. streamAllocation.eventListener.responseBodyStart(streamAllocation.call);
  3. String contentType = response.header("Content-Type");
  4. if (!HttpHeaders.hasBody(response)) {
  5. Source source = newFixedLengthSource(0);
  6. return new RealResponseBody(contentType, 0, Okio.buffer(source));
  7. }
  8. if ("chunked".equalsIgnoreCase(response.header("Transfer-Encoding"))) {
  9. Source source = newChunkedSource(response.request().url());
  10. return new RealResponseBody(contentType, -1L, Okio.buffer(source));
  11. }
  12. long contentLength = HttpHeaders.contentLength(response);
  13. if (contentLength != -1) {
  14. Source source = newFixedLengthSource(contentLength);
  15. return new RealResponseBody(contentType, contentLength, Okio.buffer(source));
  16. }
  17. return new RealResponseBody(contentType, -1L, Okio.buffer(newUnknownLengthSource()));
  18. }

相关文章