Search in sources :

Example 6 with Builder

use of okhttp3.Request.Builder in project MVCHelper by LuckyJayce.

the class PostFileMethod method buildRequset.

@Override
protected Request.Builder buildRequset(String url, Map<String, Object> params) {
    MultipartBody.Builder builder = new MultipartBody.Builder();
    if (params != null) {
        for (Entry<String, ?> entry : params.entrySet()) {
            builder.addFormDataPart(entry.getKey(), String.valueOf(entry.getValue()));
        }
    }
    if (httpbodys != null) {
        for (Entry<String, Data2<String, RequestBody>> entry : httpbodys.entrySet()) {
            String key = entry.getKey();
            Data2<String, RequestBody> body = entry.getValue();
            builder.addFormDataPart(key, body.getValue1(), body.getValue2());
        }
    }
    RequestBody formBody = builder.build();
    if (listener != null) {
        formBody = new CountingRequestBody(formBody, listener);
    }
    return new Request.Builder().url(url).post(formBody);
}
Also used : Data2(com.shizhefei.mvc.data.Data2) MultipartBody(okhttp3.MultipartBody) RequestBody(okhttp3.RequestBody)

Example 7 with Builder

use of okhttp3.Request.Builder in project MVCHelper by LuckyJayce.

the class PostMethod method buildRequset.

@Override
protected Request.Builder buildRequset(String url, Map<String, Object> params) {
    FormBody.Builder builder = new FormBody.Builder();
    if (params != null) {
        for (Entry<String, ?> entry : params.entrySet()) {
            builder.add(entry.getKey(), String.valueOf(entry.getValue()));
        }
    }
    RequestBody formBody = builder.build();
    return new Request.Builder().url(url).post(formBody);
}
Also used : FormBody(okhttp3.FormBody) RequestBody(okhttp3.RequestBody)

Example 8 with Builder

use of okhttp3.Request.Builder in project MVCHelper by LuckyJayce.

the class PutMethod method buildRequset.

@Override
protected Request.Builder buildRequset(String url, Map<String, Object> params) {
    FormBody.Builder builder = new FormBody.Builder();
    if (params != null) {
        for (Entry<String, ?> entry : params.entrySet()) {
            builder.add(entry.getKey(), String.valueOf(entry.getValue()));
        }
    }
    RequestBody formBody = builder.build();
    return new Request.Builder().url(url).put(formBody);
}
Also used : FormBody(okhttp3.FormBody) RequestBody(okhttp3.RequestBody)

Example 9 with Builder

use of okhttp3.Request.Builder in project sonarqube by SonarSource.

the class OkHttpClientBuilder method build.

public OkHttpClient build() {
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.proxy(proxy);
    if (connectTimeoutMs >= 0) {
        builder.connectTimeout(connectTimeoutMs, TimeUnit.MILLISECONDS);
    }
    if (readTimeoutMs >= 0) {
        builder.readTimeout(readTimeoutMs, TimeUnit.MILLISECONDS);
    }
    builder.addInterceptor(this::completeHeaders);
    ConnectionSpec tls = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS).allEnabledTlsVersions().allEnabledCipherSuites().supportsTlsExtensions(true).build();
    builder.connectionSpecs(asList(tls, ConnectionSpec.CLEARTEXT));
    X509TrustManager trustManager = sslTrustManager != null ? sslTrustManager : systemDefaultTrustManager();
    SSLSocketFactory sslFactory = sslSocketFactory != null ? sslSocketFactory : systemDefaultSslSocketFactory(trustManager);
    builder.sslSocketFactory(sslFactory, trustManager);
    return builder.build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) ConnectionSpec(okhttp3.ConnectionSpec) X509TrustManager(javax.net.ssl.X509TrustManager) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 10 with Builder

use of okhttp3.Request.Builder in project Parse-SDK-Android by ParsePlatform.

the class ParseOkHttpClientTest method testGetOkHttpRequestType.

//region testTransferRequest/Response
@Test
public void testGetOkHttpRequestType() throws IOException {
    ParseOkHttpClient parseClient = new ParseOkHttpClient(10000, null);
    ParseHttpRequest.Builder builder = new ParseHttpRequest.Builder();
    builder.setUrl("http://www.parse.com");
    // Get
    ParseHttpRequest parseRequest = builder.setMethod(ParseHttpRequest.Method.GET).setBody(null).build();
    Request okHttpRequest = parseClient.getRequest(parseRequest);
    assertEquals(ParseHttpRequest.Method.GET.toString(), okHttpRequest.method());
    // Post
    parseRequest = builder.setMethod(ParseHttpRequest.Method.POST).setBody(new ParseByteArrayHttpBody("test", "application/json")).build();
    okHttpRequest = parseClient.getRequest(parseRequest);
    assertEquals(ParseHttpRequest.Method.POST.toString(), okHttpRequest.method());
    // Delete
    parseRequest = builder.setMethod(ParseHttpRequest.Method.DELETE).setBody(null).build();
    okHttpRequest = parseClient.getRequest(parseRequest);
    assertEquals(ParseHttpRequest.Method.DELETE.toString(), okHttpRequest.method());
    // Put
    parseRequest = builder.setMethod(ParseHttpRequest.Method.PUT).setBody(new ParseByteArrayHttpBody("test", "application/json")).build();
    okHttpRequest = parseClient.getRequest(parseRequest);
    assertEquals(ParseHttpRequest.Method.PUT.toString(), okHttpRequest.method());
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) Request(okhttp3.Request) ParseHttpRequest(com.parse.http.ParseHttpRequest) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Aggregations

Request (okhttp3.Request)163 Response (okhttp3.Response)120 OkHttpClient (okhttp3.OkHttpClient)91 IOException (java.io.IOException)83 RequestBody (okhttp3.RequestBody)67 Test (org.junit.Test)67 MultipartBody (okhttp3.MultipartBody)35 File (java.io.File)31 Map (java.util.Map)31 MockResponse (okhttp3.mockwebserver.MockResponse)31 HttpUrl (okhttp3.HttpUrl)29 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)27 Call (okhttp3.Call)25 Interceptor (okhttp3.Interceptor)24 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)22 Retrofit (retrofit2.Retrofit)20 Builder (okhttp3.Request.Builder)19 ResponseBody (okhttp3.ResponseBody)18 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)17 Builder (okhttp3.OkHttpClient.Builder)17