本文整理汇总了Java中okhttp3.internal.http.HttpMethod.requiresRequestBody方法的典型用法代码示例。如果您正苦于以下问题:Java HttpMethod.requiresRequestBody方法的具体用法?Java HttpMethod.requiresRequestBody怎么用?Java HttpMethod.requiresRequestBody使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类okhttp3.internal.http.HttpMethod
的用法示例。
在下文中一共展示了HttpMethod.requiresRequestBody方法的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: 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;
}
示例3: 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();
}
示例4: 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();
}
示例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, null);
builder.headers(headers);
}
return builder.build();
}
示例6: 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();
}
示例7: 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;
}
示例8: 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();
}
示例9: 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(TlsVersion.SSL_3_0, cipherSuite,
nullSafeImmutableList(peerCertificates), nullSafeImmutableList(localCertificates));
okResponseBuilder.handshake(handshake);
}
return okResponseBuilder.build();
}
示例10: 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;
}
示例11: checkMethodAndBody
import okhttp3.internal.http.HttpMethod; //导入方法依赖的package包/类
protected void checkMethodAndBody() {
if (method == null) {
throw new NullPointerException("method == null");
}
if (method.length() == 0) {
throw new IllegalArgumentException("method.length() == 0");
}
if (body != null && !HttpMethod.permitsRequestBody(method)) {
throw new IllegalArgumentException("method " + method + " must not have a request body.");
}
if (body == null && HttpMethod.requiresRequestBody(method)) {
this.body = Util.EMPTY_REQUEST;
}
}
示例12: build
import okhttp3.internal.http.HttpMethod; //导入方法依赖的package包/类
@Override
public OtherRequest build() {
if (Objects.isNull(requestBody) && HttpMethod.requiresRequestBody(method)) {
throw new NullPointerException(
String.format("requestBody can not be null in method: %s.", method));
}
return new OtherRequest(this);
}
示例13: concatRequestBody
import okhttp3.internal.http.HttpMethod; //导入方法依赖的package包/类
private RequestBody concatRequestBody(String method, RequestBody firstRequestBody, ArrayList<RequestBody> secondRequestBodyList, List<RequestBodyAppender> appenders) {
if (secondRequestBodyList == null
|| secondRequestBodyList.isEmpty()) {
return firstRequestBody;
}
if (firstRequestBody == null) {
return HttpMethod.requiresRequestBody(method)
? secondRequestBodyList.get(0)
: null;
}
if (appenders == null
|| appenders.isEmpty()) {
System.err.println(">>>> " + TAG + " ERROR:");
System.err.println("can't find RequestBodyAppender, cancel append global http params, please check the code");
System.err.println("<<<<");
return firstRequestBody;
}
for (RequestBodyAppender appender : appenders) {
for (RequestBody appenderRequestBody : secondRequestBodyList) {
if (appender.isAccept(firstRequestBody, appenderRequestBody)) {
return appender.append(firstRequestBody, appenderRequestBody);
}
}
}
System.err.println(">>>> " + TAG + " ERROR:");
System.err.println(
"can't find proper RequestBodyAppender for RequestBody '"
+ firstRequestBody.getClass().getSimpleName()
+ "', cancel append global http params, please check the code"
);
System.err.println("<<<<");
return firstRequestBody;
}
示例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)
? EMPTY_REQUEST_BODY
: 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.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: newHttpEngine
import okhttp3.internal.http.HttpMethod; //导入方法依赖的package包/类
private HttpEngine newHttpEngine(String method, StreamAllocation streamAllocation,
RetryableSink requestBody, Response priorResponse)
throws MalformedURLException, UnknownHostException {
// OkHttp's Call API requires a placeholder body; the real body will be streamed separately.
RequestBody placeholderBody = HttpMethod.requiresRequestBody(method)
? EMPTY_REQUEST_BODY
: null;
URL url = getURL();
HttpUrl httpUrl = Internal.instance.getHttpUrlChecked(url.toString());
Request.Builder builder = new Request.Builder()
.url(httpUrl)
.method(method, placeholderBody);
Headers headers = requestHeaders.build();
for (int i = 0, size = headers.size(); i < size; i++) {
builder.addHeader(headers.name(i), headers.value(i));
}
boolean bufferRequestBody = false;
if (HttpMethod.permitsRequestBody(method)) {
// Specify how the request body is terminated.
if (fixedContentLength != -1) {
builder.header("Content-Length", Long.toString(fixedContentLength));
} else if (chunkLength > 0) {
builder.header("Transfer-Encoding", "chunked");
} else {
bufferRequestBody = true;
}
// Add a content type for the request body, if one isn't already present.
if (headers.get("Content-Type") == null) {
builder.header("Content-Type", "application/x-www-form-urlencoded");
}
}
if (headers.get("User-Agent") == null) {
builder.header("User-Agent", defaultUserAgent());
}
Request request = builder.build();
// If we're currently not using caches, make sure the engine's client doesn't have one.
OkHttpClient engineClient = client;
if (Internal.instance.internalCache(engineClient) != null && !getUseCaches()) {
engineClient = client.newBuilder()
.cache(null)
.build();
}
return new HttpEngine(engineClient, request, bufferRequestBody, true, false, streamAllocation,
requestBody, priorResponse);
}