本文整理汇总了Java中okhttp3.internal.http.HttpMethod类的典型用法代码示例。如果您正苦于以下问题:Java HttpMethod类的具体用法?Java HttpMethod怎么用?Java HttpMethod使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpMethod类属于okhttp3.internal.http包,在下文中一共展示了HttpMethod类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createOkRequest
import okhttp3.internal.http.HttpMethod; //导入依赖的package包/类
/**
* Creates an OkHttp {@link Request} from the supplied information.
*
* <p>This method allows a {@code null} value for {@code requestHeaders} for situations where a
* connection is already connected and access to the headers has been lost. See {@link
* java.net.HttpURLConnection#getRequestProperties()} for details.
*/
public static Request createOkRequest(
URI uri, String requestMethod, Map<String, List<String>> requestHeaders) {
// OkHttp's Call API requires a placeholder body; the real body will be streamed separately.
RequestBody placeholderBody = HttpMethod.requiresRequestBody(requestMethod)
? Util.EMPTY_REQUEST
: null;
Request.Builder builder = new Request.Builder()
.url(uri.toString())
.method(requestMethod, placeholderBody);
if (requestHeaders != null) {
Headers headers = extractOkHeaders(requestHeaders, null);
builder.headers(headers);
}
return builder.build();
}
示例2: maybeCache
import okhttp3.internal.http.HttpMethod; //导入依赖的package包/类
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);
}
示例3: buildRequestBody
import okhttp3.internal.http.HttpMethod; //导入依赖的package包/类
@Override
protected RequestBody buildRequestBody()
{
if (requestBody == null && TextUtils.isEmpty(content) && HttpMethod.requiresRequestBody(method))
{
LogUtils.w("requestBody and content can not be null in method:" + 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;
}
示例4: toImmutable
import okhttp3.internal.http.HttpMethod; //导入依赖的package包/类
@Override Request toImmutable() {
Object body = body();
MediaType mediaType = MediaType.parse(contentType());
RequestBody requestBody = null;
if (body != null) {
requestBody = body instanceof String
? RequestBody.create(mediaType, (String) body)
: RequestBody.create(mediaType, (byte[]) body);
} else if (HttpMethod.requiresRequestBody(method)) {
// The method required a body but none was given. Use an empty one.
requestBody = RequestBody.create(mediaType, new byte[0]);
}
return new RecordedRequest.Builder()
.headers(okhttp3.Headers.of(headers()))
.method(method, requestBody)
.url(HttpUrl.get(uri))
.build();
}
示例5: createOkRequest
import okhttp3.internal.http.HttpMethod; //导入依赖的package包/类
/**
* Creates an OkHttp {@link Request} from the supplied information.
*
* <p>This method allows a {@code null} value for {@code requestHeaders} for situations where a
* connection is already connected and access to the headers has been lost. See {@link
* java.net.HttpURLConnection#getRequestProperties()} for details.
*/
public static Request createOkRequest(
URI uri, String requestMethod, Map<String, List<String>> requestHeaders) {
// OkHttp's Call API requires a placeholder body; the real body will be streamed separately.
RequestBody placeholderBody = HttpMethod.requiresRequestBody(requestMethod)
? EMPTY_REQUEST_BODY
: null;
Request.Builder builder = new Request.Builder()
.url(uri.toString())
.method(requestMethod, placeholderBody);
if (requestHeaders != null) {
Headers headers = extractOkHeaders(requestHeaders);
builder.headers(headers);
}
return builder.build();
}
示例6: initHttpEngine
import okhttp3.internal.http.HttpMethod; //导入依赖的package包/类
private void initHttpEngine() throws IOException {
if (httpEngineFailure != null) {
throw httpEngineFailure;
} else if (httpEngine != null) {
return;
}
connected = true;
try {
if (doOutput) {
if (method.equals("GET")) {
// they are requesting a stream to write to. This implies a POST method
method = "POST";
} else if (!HttpMethod.permitsRequestBody(method)) {
throw new ProtocolException(method + " does not support writing");
}
}
// If the user set content length to zero, we know there will not be a request body.
httpEngine = newHttpEngine(method, null, null, null);
} catch (IOException e) {
httpEngineFailure = e;
throw e;
}
}
示例7: createOkRequest
import okhttp3.internal.http.HttpMethod; //导入依赖的package包/类
/**
* Creates an OkHttp {@link Request} from the supplied information.
*
* <p>This method allows a {@code null} value for {@code requestHeaders} for situations where a
* connection is already connected and access to the headers has been lost. See {@link
* java.net.HttpURLConnection#getRequestProperties()} for details.
*/
public static Request createOkRequest(
URI uri, String requestMethod, Map<String, List<String>> requestHeaders) {
// OkHttp's Call API requires a placeholder body; the real body will be streamed separately.
RequestBody placeholderBody = HttpMethod.requiresRequestBody(requestMethod)
? EMPTY_REQUEST_BODY
: null;
Request.Builder builder = new Request.Builder()
.url(uri.toString())
.method(requestMethod, placeholderBody);
if (requestHeaders != null) {
Headers headers = extractOkHeaders(requestHeaders, null);
builder.headers(headers);
}
return builder.build();
}
示例8: maybeCache
import okhttp3.internal.http.HttpMethod; //导入依赖的package包/类
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);
}
示例9: execute
import okhttp3.internal.http.HttpMethod; //导入依赖的package包/类
protected Response execute(Request request) throws IOException {
if (Http.sCache != null) {
if (HttpMethod.invalidatesCache(request.method())) {
try {
Http.sCache.remove(getUrl());
} catch (IOException ignore) {
}
}
if (!"GET".equals(request.method())) {
setShouldCache(false);
}
}
OkHttpClient client = getClient();
Call call = client.newCall(request);
long start = System.currentTimeMillis();
Response response = call.execute();
if (LOG.isLoggable(Log.INFO))
LOG.i("Took " + (System.currentTimeMillis() - start) + "ms to execute request (" + getLogKey() + ")");
if (Http.sCache != null && HttpHeaders.hasVaryAll(response)) {
setShouldCache(false);
}
return response;
}
示例10: getRequestBody
import okhttp3.internal.http.HttpMethod; //导入依赖的package包/类
@Override
public RequestBody getRequestBody(String contentType, long contentLength, String method,
Map<String, Object> bodyParams, Map<String, Object> queryParams) throws QSException {
logger.info("----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;
}
示例11: intercept
import okhttp3.internal.http.HttpMethod; //导入依赖的package包/类
@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);
}
示例12: roundtrip
import okhttp3.internal.http.HttpMethod; //导入依赖的package包/类
private Response roundtrip() throws IOException {
RequestBody body = null;
HttpUrl.Builder urlBuilder = httpUrl.newBuilder();
if (params != null) {
ImmutableMap<String, String> queries = params.query(serializer);
if (queries != null && !queries.isEmpty()) {
for (ImmutableMap.Entry<String, String> pair : queries.entrySet()) {
urlBuilder = urlBuilder.addQueryParameter(pair.getKey(), pair.getValue());
}
}
body = params.body(serializer);
} else if (HttpMethod.requiresRequestBody(method)) { // params == null
body = new FormBody.Builder().build();
}
Request request = new Request.Builder()
.method(method, body)
.url(urlBuilder.build())
.build();
return httpClient.newCall(request).execute();
}
示例13: buildRequestBody
import okhttp3.internal.http.HttpMethod; //导入依赖的package包/类
@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;
}
示例14: createOkResponseForCachePut
import okhttp3.internal.http.HttpMethod; //导入依赖的package包/类
/**
* Creates an OkHttp {@link Response} using the supplied {@link URI} and {@link URLConnection} to
* supply the data. The URLConnection is assumed to already be connected. If this method returns
* {@code null} the response is uncacheable.
*/
public static Response createOkResponseForCachePut(URI uri, URLConnection urlConnection)
throws IOException {
HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection;
Response.Builder okResponseBuilder = new Response.Builder();
// Request: Create one from the URL connection.
Headers responseHeaders = createHeaders(urlConnection.getHeaderFields());
// Some request headers are needed for Vary caching.
Headers varyHeaders = varyHeaders(urlConnection, responseHeaders);
if (varyHeaders == null) {
return null;
}
// OkHttp's Call API requires a placeholder body; the real body will be streamed separately.
String requestMethod = httpUrlConnection.getRequestMethod();
RequestBody placeholderBody = HttpMethod.requiresRequestBody(requestMethod)
? Util.EMPTY_REQUEST
: null;
Request okRequest = new Request.Builder()
.url(uri.toString())
.method(requestMethod, placeholderBody)
.headers(varyHeaders)
.build();
okResponseBuilder.request(okRequest);
// Status line
StatusLine statusLine = StatusLine.parse(extractStatusLine(httpUrlConnection));
okResponseBuilder.protocol(statusLine.protocol);
okResponseBuilder.code(statusLine.code);
okResponseBuilder.message(statusLine.message);
// A network response is required for the Cache to find any Vary headers it needs.
Response networkResponse = okResponseBuilder.build();
okResponseBuilder.networkResponse(networkResponse);
// Response headers
Headers okHeaders = extractOkResponseHeaders(httpUrlConnection, okResponseBuilder);
okResponseBuilder.headers(okHeaders);
// Response body
ResponseBody okBody = createOkBody(urlConnection);
okResponseBuilder.body(okBody);
// Handle SSL handshake information as needed.
if (httpUrlConnection instanceof HttpsURLConnection) {
HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) httpUrlConnection;
Certificate[] peerCertificates;
try {
peerCertificates = httpsUrlConnection.getServerCertificates();
} catch (SSLPeerUnverifiedException e) {
peerCertificates = null;
}
Certificate[] localCertificates = httpsUrlConnection.getLocalCertificates();
String cipherSuiteString = httpsUrlConnection.getCipherSuite();
CipherSuite cipherSuite = CipherSuite.forJavaName(cipherSuiteString);
Handshake handshake = Handshake.get(null, cipherSuite,
nullSafeImmutableList(peerCertificates), nullSafeImmutableList(localCertificates));
okResponseBuilder.handshake(handshake);
}
return okResponseBuilder.build();
}
示例15: put
import okhttp3.internal.http.HttpMethod; //导入依赖的package包/类
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;
}
}