Tabnine Logo
okhttp3
Code IndexAdd Tabnine to your IDE (free)

How to use okhttp3

Best Java code snippets using okhttp3 (Showing top 20 results out of 315)

origin: square/okhttp

String post(String url, String json) throws IOException {
 RequestBody body = RequestBody.create(JSON, json);
 Request request = new Request.Builder()
   .url(url)
   .post(body)
   .build();
 try (Response response = client.newCall(request).execute()) {
  return response.body().string();
 }
}
origin: square/okhttp

String run(String url) throws IOException {
 Request request = new Request.Builder()
   .url(url)
   .build();
 try (Response response = client.newCall(request).execute()) {
  return response.body().string();
 }
}
origin: square/okhttp

public void run() throws Exception {
 Request request = new Request.Builder()
   .url("https://publicobject.com/helloworld.txt")
   .build();
 Response response = client.newCall(request).execute();
 response.body().close();
}
origin: square/okhttp

private void run() {
 OkHttpClient client = new OkHttpClient.Builder()
   .readTimeout(0,  TimeUnit.MILLISECONDS)
   .build();
 Request request = new Request.Builder()
   .url("ws://echo.websocket.org")
   .build();
 client.newWebSocket(request, this);
 // Trigger shutdown of the dispatcher's executor so this process can exit cleanly.
 client.dispatcher().executorService().shutdown();
}
origin: square/okhttp

public void run() throws Exception {
 Request request = new Request.Builder()
   .url("https://publicobject.com/helloworld.txt")
   .build();
 try (Response response = client.newCall(request).execute()) {
  System.out.println(response.request().header("Date"));
 }
}
origin: square/okhttp

public CacheResponse(File cacheDirectory) throws Exception {
 int cacheSize = 10 * 1024 * 1024; // 10 MiB
 Cache cache = new Cache(cacheDirectory, cacheSize);
 client = new OkHttpClient.Builder()
   .cache(cache)
   .build();
}
origin: square/okhttp

public CertificatePinning() {
 client = new OkHttpClient.Builder()
   .certificatePinner(
     new CertificatePinner.Builder()
       .add("publicobject.com", "sha256/afwiKY3RxoMmLkuRW1l7QsPZTJPwDS2pdDROQjXw8ig=")
       .build())
   .build();
}
origin: square/okhttp

@Override public void setConnectTimeout(int timeoutMillis) {
 client = client.newBuilder()
   .connectTimeout(timeoutMillis, TimeUnit.MILLISECONDS)
   .build();
}
origin: square/okhttp

private static Response stripBody(Response response) {
 return response != null && response.body() != null
   ? response.newBuilder().body(null).build()
   : response;
}
origin: square/okhttp

@Override public void setHostnameVerifier(HostnameVerifier hostnameVerifier) {
 delegate.client = delegate.client.newBuilder()
   .hostnameVerifier(hostnameVerifier)
   .build();
}
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

/**
 * Returns a string with containing this URL with its username, password, query, and fragment
 * stripped, and its path replaced with {@code /...}. For example, redacting {@code
 * http://username:password@example.com/path} returns {@code http://example.com/...}.
 */
public String redact() {
 return newBuilder("/...")
   .username("")
   .password("")
   .build()
   .toString();
}
origin: square/okhttp

@Override
public MediaType contentType() {
 String contentTypeHeader = okHeaders.get("Content-Type");
 return contentTypeHeader == null ? null : MediaType.parse(contentTypeHeader);
}
origin: square/okhttp

private Headers getHeaders() throws IOException {
 if (responseHeaders == null) {
  Response response = getResponse(true);
  Headers headers = response.headers();
  responseHeaders = headers.newBuilder()
    .add(SELECTED_PROTOCOL, response.protocol().toString())
    .add(RESPONSE_SOURCE, responseSourceHeader(response))
    .build();
 }
 return responseHeaders;
}
origin: square/okhttp

/**
 * Adds all headers from an existing collection.
 */
public Builder addAll(Headers headers) {
 for (int i = 0, size = headers.size(); i < size; i++) {
  addLenient(headers.name(i), headers.value(i));
 }
 return this;
}
origin: square/okhttp

/**
 * Returns true if computeFreshnessLifetime used a heuristic. If we used a heuristic to serve a
 * cached response older than 24 hours, we are required to attach a warning.
 */
private boolean isFreshnessLifetimeHeuristic() {
 return cacheResponse.cacheControl().maxAgeSeconds() == -1 && expires == null;
}
origin: square/okhttp

/**
 * Add a header with the specified name and value. Does validation of header names and values.
 */
public Builder add(String name, String value) {
 checkName(name);
 checkValue(value, name);
 return addLenient(name, value);
}
origin: square/okhttp

static RealCall newRealCall(OkHttpClient client, Request originalRequest, boolean forWebSocket) {
 // Safely publish the Call instance to the EventListener.
 RealCall call = new RealCall(client, originalRequest, forWebSocket);
 call.eventListener = client.eventListenerFactory().create(call);
 return call;
}
origin: square/okhttp

/**
 * Add a header with the specified name and value. Does validation of header names, allowing
 * non-ASCII values.
 */
public Builder addUnsafeNonAscii(String name, String value) {
 checkName(name);
 return addLenient(name, value);
}
origin: square/okhttp

public RewriteResponseCacheControl(File cacheDirectory) throws Exception {
 Cache cache = new Cache(cacheDirectory, 1024 * 1024);
 cache.evictAll();
 client = new OkHttpClient.Builder()
   .cache(cache)
   .build();
}
okhttp3

Most used classes

  • Request$Builder
  • OkHttpClient$Builder
  • ResponseBody
    A one-shot stream from the origin server to the client application with the raw bytes of the respons
  • Response
    An HTTP response. Instances of this class are not immutable: the response body is a one-shot value t
  • OkHttpClient
    Factory for Call, which can be used to send HTTP requests and read their responses.OKHTTPCLIENTS SHO
  • Request,
  • RequestBody,
  • HttpUrl,
  • Interceptor$Chain,
  • MediaType,
  • Headers,
  • HttpLoggingInterceptor,
  • Response$Builder,
  • HttpUrl$Builder,
  • Cache,
  • FormBody$Builder,
  • MockWebServer,
  • MultipartBody$Builder,
  • MockResponse
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now