okhttp3.internal.http.HttpMethod Java Examples
The following examples show how to use
okhttp3.internal.http.HttpMethod.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CacheInterceptor.java From AndroidProjects with MIT License | 7 votes |
private CacheRequest maybeCache(Response userResponse, Request networkRequest, InternalCache responseCache) throws IOException { if (responseCache == null) return null; // Should we cache this response for this request? if (!CacheStrategy.isCacheable(userResponse, networkRequest)) { if (HttpMethod.invalidatesCache(networkRequest.method())) { try { responseCache.remove(networkRequest); } catch (IOException ignored) { // The cache cannot be written. } } return null; } // Offer this request to the cache. return responseCache.put(userResponse); }
Example #2
Source File: StaleIfErrorInterceptor.java From edx-app-android with Apache License 2.0 | 6 votes |
@Override public Response intercept(@NonNull final Chain chain) throws IOException { Request request = chain.request(); // Verify that the HTTP method is for loading data only and doesn't have any side-effects, // and that the request doesn't contain the 'only-if-cached' Cache-Control directive to // force loading from the cache. if (!HttpMethod.invalidatesCache(request.method()) && !request.cacheControl().onlyIfCached()) { // If the request already has the 'stale-if-error' Cache-Control directive, then proceed // the request chain without interference. for (final String cacheControlValue : request.headers("Cache-Control")) { if (PATTERN_STALE_IF_ERROR.matcher(cacheControlValue).matches()) { return chain.proceed(request); } } // Otherwise add a 'stale-if-error' Cache-Control directive, with the maximum stale // value set to a very high value. request = request.newBuilder() .addHeader("Cache-Control", "stale-if-error=" + Integer.MAX_VALUE) .build(); } return chain.proceed(request); }
Example #3
Source File: ResponseHeaderRecord.java From apollo-android with MIT License | 6 votes |
Response response() { RequestBody body = null; if (HttpMethod.permitsRequestBody(requestMethod)) { body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), ""); } Request cacheRequest = new Request.Builder() .url(url) .method(requestMethod, body) .headers(varyHeaders) .build(); return new Response.Builder() .request(cacheRequest) .protocol(protocol) .code(code) .message(message) .headers(responseHeaders) .handshake(handshake) .sentRequestAtMillis(sentRequestMillis) .receivedResponseAtMillis(receivedResponseMillis) .build(); }
Example #4
Source File: QSNormalRequestBody.java From qingstor-sdk-java with Apache License 2.0 | 5 votes |
@Override public RequestBody getRequestBody( String contentType, long contentLength, String method, Map<String, Object> bodyParams, Map<String, Object> queryParams) throws QSException { log.debug("----QSNormalRequestBody----"); MediaType mediaType = MediaType.parse(contentType); if (bodyParams != null && bodyParams.size() > 0) { RequestBody body = null; Object bodyObj = getBodyContent(bodyParams); if (bodyObj instanceof String) { body = RequestBody.create(mediaType, bodyObj.toString()); } else if (bodyObj instanceof File) { body = RequestBody.create(mediaType, (File) bodyObj); } else if (bodyObj instanceof InputStream) { body = new InputStreamUploadBody(contentType, (InputStream) bodyObj, contentLength); } return body; // connection.getOutputStream().write(bodyContent.getBytes()); } else { if (HttpMethod.permitsRequestBody(method)) { return new EmptyRequestBody(contentType); } } return null; }
Example #5
Source File: RequesterImpl.java From omise-java with MIT License | 5 votes |
/** * Carries out the HTTP request * * @param url api url path * @param body additional request params (if available) * @param method HTTP method * @return {@link Response} response object returned by {@link OkHttpClient} * @throws IOException I/O exception thrown by {@link OkHttpClient} during the HTTP request */ private Response roundTrip(HttpUrl url, RequestBody body, String method) throws IOException { if (body == null && HttpMethod.requiresRequestBody(method)) { // params == null body = new FormBody.Builder().build(); } okhttp3.Request request = new okhttp3.Request.Builder() .method(method, body) .url(url) .build(); return httpClient.newCall(request).execute(); }
Example #6
Source File: Requester.java From JDA with Apache License 2.0 | 5 votes |
private void applyBody(Request<?> apiRequest, okhttp3.Request.Builder builder) { String method = apiRequest.getRoute().getMethod().toString(); RequestBody body = apiRequest.getBody(); if (body == null && HttpMethod.requiresRequestBody(method)) body = EMPTY_BODY; builder.method(method, body); }
Example #7
Source File: OtherRequest.java From okhttputils with Apache License 2.0 | 5 votes |
@Override protected RequestBody buildRequestBody() { if (requestBody == null && TextUtils.isEmpty(content) && HttpMethod.requiresRequestBody(method)) { Exceptions.illegalArgument("requestBody and content can not be null in method:" + method); } if (requestBody == null && !TextUtils.isEmpty(content)) { requestBody = RequestBody.create(MEDIA_TYPE_PLAIN, content); } return requestBody; }
Example #8
Source File: OkOtherRequest.java From FamilyChat with Apache License 2.0 | 5 votes |
@Override protected RequestBody buildRequestBody() { if (requestBody == null && TextUtils.isEmpty(content) && HttpMethod.requiresRequestBody(method)) throw new IllegalArgumentException("requestBody and content can not be null in method:" + method); if (requestBody == null && !TextUtils.isEmpty(content)) requestBody = RequestBody.create(MEDIA_TYPE_PLAIN, content); return requestBody; }
Example #9
Source File: Cache.java From AndroidProjects with MIT License | 5 votes |
CacheRequest put(Response response) { String requestMethod = response.request().method(); if (HttpMethod.invalidatesCache(response.request().method())) { try { remove(response.request()); } catch (IOException ignored) { // The cache cannot be written. } return null; } if (!requestMethod.equals("GET")) { // Don't cache non-GET responses. We're technically allowed to cache // HEAD requests and some POST requests, but the complexity of doing // so is high and the benefit is low. return null; } if (HttpHeaders.hasVaryAll(response)) { return null; } Entry entry = new Entry(response); DiskLruCache.Editor editor = null; try { editor = cache.edit(key(response.request().url())); if (editor == null) { return null; } entry.writeTo(editor); return new CacheRequestImpl(editor); } catch (IOException e) { abortQuietly(editor); return null; } }
Example #10
Source File: OtherRequest.java From enjoyshop with Apache License 2.0 | 5 votes |
@Override protected RequestBody buildRequestBody() { if (requestBody == null && TextUtils.isEmpty(content) && HttpMethod.requiresRequestBody(method)) { Exceptions.illegalArgument("requestBody and content can not be null in method:" + method); } if (requestBody == null && !TextUtils.isEmpty(content)) { requestBody = RequestBody.create(MEDIA_TYPE_PLAIN, content); } return requestBody; }
Example #11
Source File: OtherRequest.java From SmartChart with Apache License 2.0 | 5 votes |
@Override protected RequestBody buildRequestBody() { if (requestBody == null && TextUtils.isEmpty(content) && HttpMethod.requiresRequestBody(method)) { Exceptions.illegalArgument("requestBody and content can not be null in method:" + method); } if (requestBody == null && !TextUtils.isEmpty(content)) { requestBody = RequestBody.create(MEDIA_TYPE_PLAIN, content); } return requestBody; }
Example #12
Source File: OtherRequest.java From stynico with MIT License | 5 votes |
@Override protected RequestBody buildRequestBody() { if (requestBody == null && TextUtils.isEmpty(content) && HttpMethod.requiresRequestBody(method)) { Exceptions.illegalArgument("requestBody and content can not be null in method:" + method); } if (requestBody == null && !TextUtils.isEmpty(content)) { requestBody = RequestBody.create(MEDIA_TYPE_PLAIN, content); } return requestBody; }
Example #13
Source File: Cache.java From styT with Apache License 2.0 | 5 votes |
CacheRequest put(Response response) { String requestMethod = response.request().method(); if (HttpMethod.invalidatesCache(response.request().method())) { try { remove(response.request()); } catch (IOException ignored) { // The cache cannot be written. } return null; } if (!requestMethod.equals("GET")) { // Don't cache non-GET responses. We're technically allowed to cache // HEAD requests and some POST requests, but the complexity of doing // so is high and the benefit is low. return null; } if (HttpHeaders.hasVaryAll(response)) { return null; } Entry entry = new Entry(response); DiskLruCache.Editor editor = null; try { editor = cache.edit(key(response.request().url())); if (editor == null) { return null; } entry.writeTo(editor); return new CacheRequestImpl(editor); } catch (IOException e) { abortQuietly(editor); return null; } }
Example #14
Source File: OtherRequest.java From styT with Apache License 2.0 | 5 votes |
@Override protected RequestBody buildRequestBody() { if (requestBody == null && TextUtils.isEmpty(content) && HttpMethod.requiresRequestBody(method)) { Exceptions.illegalArgument("requestBody and content can not be null in method:" + method); } if (requestBody == null && !TextUtils.isEmpty(content)) { requestBody = RequestBody.create(MEDIA_TYPE_PLAIN, content); } return requestBody; }
Example #15
Source File: QSMultiPartUploadRequestBody.java From qingstor-sdk-java with Apache License 2.0 | 4 votes |
@Override public RequestBody getRequestBody( String contentType, long contentLength, String method, Map<String, Object> bodyParams, Map<String, Object> queryParams) throws QSException { log.debug("----QSMultiPartUploadRequestBody---"); MediaType mediaType = MediaType.parse(contentType); if (bodyParams != null && bodyParams.size() > 0) { RequestBody requestBody = null; Iterator iterator = bodyParams.entrySet().iterator(); int partNumber = Integer.parseInt(queryParams.get(QSConstant.PARAM_KEY_PART_NUMBER) + ""); long offset = Long.parseLong(queryParams.get(QSConstant.PARAM_KEY_FILE_OFFSET) + ""); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); String key = (String) entry.getKey(); Object bodyObj = bodyParams.get(key); if (bodyObj instanceof String) { requestBody = RequestBody.create(mediaType, bodyObj.toString()); } else if (bodyObj instanceof File) { if (contentLength == 0) contentLength = ((File) bodyObj).length(); if (offset < 0) offset = contentLength * partNumber; requestBody = getSeekFileRequestBody( contentType, contentLength, offset, (File) bodyObj); } else if (bodyObj instanceof InputStream) { requestBody = new InputStreamUploadBody( contentType, (InputStream) bodyObj, contentLength, offset); } else { String jsonStr = QSStringUtil.objectToJson(key, bodyObj); requestBody = RequestBody.create(mediaType, jsonStr); } } return requestBody; // connection.getOutputStream().write(bodyContent.getBytes()); } else { if (HttpMethod.permitsRequestBody(method)) { return new EmptyRequestBody(contentType); } } return null; }
Example #16
Source File: HttpRequest.java From mica with GNU Lesser General Public License v3.0 | 4 votes |
private Call internalCall(final OkHttpClient client) { OkHttpClient.Builder builder = client.newBuilder(); if (connectTimeout != null) { builder.connectTimeout(connectTimeout.toMillis(), TimeUnit.MILLISECONDS); } if (readTimeout != null) { builder.readTimeout(readTimeout.toMillis(), TimeUnit.MILLISECONDS); } if (writeTimeout != null) { builder.writeTimeout(writeTimeout.toMillis(), TimeUnit.MILLISECONDS); } if (protocols != null && !protocols.isEmpty()) { builder.protocols(protocols); } if (proxy != null) { builder.proxy(proxy); } if (proxySelector != null) { builder.proxySelector(proxySelector); } if (proxyAuthenticator != null) { builder.proxyAuthenticator(proxyAuthenticator); } if (hostnameVerifier != null) { builder.hostnameVerifier(hostnameVerifier); } if (sslSocketFactory != null && trustManager != null) { builder.sslSocketFactory(sslSocketFactory, trustManager); } if (Boolean.TRUE.equals(disableSslValidation)) { disableSslValidation(builder); } if (authenticator != null) { builder.authenticator(authenticator); } if (eventListener != null) { builder.eventListener(eventListener); } if (!interceptors.isEmpty()) { builder.interceptors().addAll(interceptors); } if (cookieJar != null) { builder.cookieJar(cookieJar); } if (followRedirects != null) { builder.followRedirects(followRedirects); } if (followSslRedirects != null) { builder.followSslRedirects(followSslRedirects); } if (retryPolicy != null) { builder.addInterceptor(new RetryInterceptor(retryPolicy)); } if (httpLogger != null && logLevel != null && HttpLoggingInterceptor.Level.NONE != logLevel) { builder.addInterceptor(getLoggingInterceptor(httpLogger, logLevel)); } else if (globalLoggingInterceptor != null) { builder.addInterceptor(globalLoggingInterceptor); } // 设置 User-Agent requestBuilder.header("User-Agent", userAgent); // url requestBuilder.url(uriBuilder.build()); String method = httpMethod; Request request; if (HttpMethod.requiresRequestBody(method) && requestBody == null) { request = requestBuilder.method(method, Util.EMPTY_REQUEST).build(); } else { request = requestBuilder.method(method, requestBody).build(); } return builder.build().newCall(request); }
Example #17
Source File: ApiClient.java From eve-esi with Apache License 2.0 | 4 votes |
/** * Build an HTTP request with the given options. * * @param path * The sub-path of the HTTP URL * @param method * The request method, one of "GET", "HEAD", "OPTIONS", "POST", * "PUT", "PATCH" and "DELETE" * @param queryParams * The query parameters * @param collectionQueryParams * The collection query parameters * @param body * The request body object * @param headerParams * The header parameters * @param cookieParams * The cookie parameters * @param formParams * The form parameters * @param authNames * The authentications to apply * @param callback * Callback for upload/download progress * @return The HTTP request * @throws ApiException * If fail to serialize the request body object */ public Request buildRequest(String path, String method, List<Pair> queryParams, List<Pair> collectionQueryParams, Object body, Map<String, String> headerParams, Map<String, String> cookieParams, Map<String, Object> formParams, String[] authNames, ApiCallback callback) throws ApiException { updateParamsForAuth(authNames, queryParams, headerParams, cookieParams); final String url = buildUrl(path, queryParams, collectionQueryParams); final Request.Builder reqBuilder = new Request.Builder().url(url); processHeaderParams(headerParams, reqBuilder); processCookieParams(cookieParams, reqBuilder); String contentType = (String) headerParams.get("Content-Type"); // ensuring a default content type if (contentType == null) { contentType = "application/json"; } RequestBody reqBody; if (!HttpMethod.permitsRequestBody(method)) { reqBody = null; } else if ("application/x-www-form-urlencoded".equals(contentType)) { reqBody = buildRequestBodyFormEncoding(formParams); } else if ("multipart/form-data".equals(contentType)) { reqBody = buildRequestBodyMultipart(formParams); } else if (body == null) { if ("DELETE".equals(method)) { // allow calling DELETE without sending a request body reqBody = null; } else { // use an empty request body (for POST, PUT and PATCH) reqBody = RequestBody.create(MediaType.parse(contentType), ""); } } else { reqBody = serialize(body, contentType); } // Associate callback with request (if not null) so interceptor can // access it when creating ProgressResponseBody reqBuilder.tag(callback); Request request = null; if (callback != null && reqBody != null) { ProgressRequestBody progressRequestBody = new ProgressRequestBody(reqBody, callback); request = reqBuilder.method(method, progressRequestBody).build(); } else { request = reqBuilder.method(method, reqBody).build(); } return request; }
Example #18
Source File: ApiClient.java From java with Apache License 2.0 | 4 votes |
/** * Build an HTTP request with the given options. * * @param path The sub-path of the HTTP URL * @param method The request method, one of "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH" and "DELETE" * @param queryParams The query parameters * @param collectionQueryParams The collection query parameters * @param body The request body object * @param headerParams The header parameters * @param cookieParams The cookie parameters * @param formParams The form parameters * @param authNames The authentications to apply * @param callback Callback for upload/download progress * @return The HTTP request * @throws ApiException If fail to serialize the request body object */ public Request buildRequest(String path, String method, List<Pair> queryParams, List<Pair> collectionQueryParams, Object body, Map<String, String> headerParams, Map<String, String> cookieParams, Map<String, Object> formParams, String[] authNames, ApiCallback callback) throws ApiException { updateParamsForAuth(authNames, queryParams, headerParams, cookieParams); final String url = buildUrl(path, queryParams, collectionQueryParams); final Request.Builder reqBuilder = new Request.Builder().url(url); processHeaderParams(headerParams, reqBuilder); processCookieParams(cookieParams, reqBuilder); String contentType = (String) headerParams.get("Content-Type"); // ensuring a default content type if (contentType == null) { contentType = "application/json"; } RequestBody reqBody; if (!HttpMethod.permitsRequestBody(method)) { reqBody = null; } else if ("application/x-www-form-urlencoded".equals(contentType)) { reqBody = buildRequestBodyFormEncoding(formParams); } else if ("multipart/form-data".equals(contentType)) { reqBody = buildRequestBodyMultipart(formParams); } else if (body == null) { if ("DELETE".equals(method)) { // allow calling DELETE without sending a request body reqBody = null; } else { // use an empty request body (for POST, PUT and PATCH) reqBody = RequestBody.create(MediaType.parse(contentType), ""); } } else { reqBody = serialize(body, contentType); } // Associate callback with request (if not null) so interceptor can // access it when creating ProgressResponseBody reqBuilder.tag(callback); Request request = null; if (callback != null && reqBody != null) { ProgressRequestBody progressRequestBody = new ProgressRequestBody(reqBody, callback); request = reqBuilder.method(method, progressRequestBody).build(); } else { request = reqBuilder.method(method, reqBody).build(); } return request; }
Example #19
Source File: ApiClient.java From openapi-generator with Apache License 2.0 | 4 votes |
/** * Build an HTTP request with the given options. * * @param path The sub-path of the HTTP URL * @param method The request method, one of "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH" and "DELETE" * @param queryParams The query parameters * @param collectionQueryParams The collection query parameters * @param body The request body object * @param headerParams The header parameters * @param cookieParams The cookie parameters * @param formParams The form parameters * @param authNames The authentications to apply * @param callback Callback for upload/download progress * @return The HTTP request * @throws ApiException If fail to serialize the request body object */ public Request buildRequest(String path, String method, List<Pair> queryParams, List<Pair> collectionQueryParams, Object body, Map<String, String> headerParams, Map<String, String> cookieParams, Map<String, Object> formParams, String[] authNames, ApiCallback callback) throws ApiException { updateParamsForAuth(authNames, queryParams, headerParams, cookieParams); final String url = buildUrl(path, queryParams, collectionQueryParams); final Request.Builder reqBuilder = new Request.Builder().url(url); processHeaderParams(headerParams, reqBuilder); processCookieParams(cookieParams, reqBuilder); String contentType = (String) headerParams.get("Content-Type"); // ensuring a default content type if (contentType == null) { contentType = "application/json"; } RequestBody reqBody; if (!HttpMethod.permitsRequestBody(method)) { reqBody = null; } else if ("application/x-www-form-urlencoded".equals(contentType)) { reqBody = buildRequestBodyFormEncoding(formParams); } else if ("multipart/form-data".equals(contentType)) { reqBody = buildRequestBodyMultipart(formParams); } else if (body == null) { if ("DELETE".equals(method)) { // allow calling DELETE without sending a request body reqBody = null; } else { // use an empty request body (for POST, PUT and PATCH) reqBody = RequestBody.create(MediaType.parse(contentType), ""); } } else { reqBody = serialize(body, contentType); } // Associate callback with request (if not null) so interceptor can // access it when creating ProgressResponseBody reqBuilder.tag(callback); Request request = null; if (callback != null && reqBody != null) { ProgressRequestBody progressRequestBody = new ProgressRequestBody(reqBody, callback); request = reqBuilder.method(method, progressRequestBody).build(); } else { request = reqBuilder.method(method, reqBody).build(); } return request; }
Example #20
Source File: ApiClient.java From openapi-generator with Apache License 2.0 | 4 votes |
/** * Build an HTTP request with the given options. * * @param path The sub-path of the HTTP URL * @param method The request method, one of "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH" and "DELETE" * @param queryParams The query parameters * @param collectionQueryParams The collection query parameters * @param body The request body object * @param headerParams The header parameters * @param cookieParams The cookie parameters * @param formParams The form parameters * @param authNames The authentications to apply * @param callback Callback for upload/download progress * @return The HTTP request * @throws ApiException If fail to serialize the request body object */ public Request buildRequest(String path, String method, List<Pair> queryParams, List<Pair> collectionQueryParams, Object body, Map<String, String> headerParams, Map<String, String> cookieParams, Map<String, Object> formParams, String[] authNames, ApiCallback callback) throws ApiException { updateParamsForAuth(authNames, queryParams, headerParams, cookieParams); final String url = buildUrl(path, queryParams, collectionQueryParams); final Request.Builder reqBuilder = new Request.Builder().url(url); processHeaderParams(headerParams, reqBuilder); processCookieParams(cookieParams, reqBuilder); String contentType = (String) headerParams.get("Content-Type"); // ensuring a default content type if (contentType == null) { contentType = "application/json"; } RequestBody reqBody; if (!HttpMethod.permitsRequestBody(method)) { reqBody = null; } else if ("application/x-www-form-urlencoded".equals(contentType)) { reqBody = buildRequestBodyFormEncoding(formParams); } else if ("multipart/form-data".equals(contentType)) { reqBody = buildRequestBodyMultipart(formParams); } else if (body == null) { if ("DELETE".equals(method)) { // allow calling DELETE without sending a request body reqBody = null; } else { // use an empty request body (for POST, PUT and PATCH) reqBody = RequestBody.create(MediaType.parse(contentType), ""); } } else { reqBody = serialize(body, contentType); } // Associate callback with request (if not null) so interceptor can // access it when creating ProgressResponseBody reqBuilder.tag(callback); Request request = null; if (callback != null && reqBody != null) { ProgressRequestBody progressRequestBody = new ProgressRequestBody(reqBody, callback); request = reqBuilder.method(method, progressRequestBody).build(); } else { request = reqBuilder.method(method, reqBody).build(); } return request; }