Search in sources :

Example 76 with Request

use of com.squareup.okhttp.Request in project blockchain-java-api-client by astarlabs.

the class ApiClient method buildCall.

/**
 * Build HTTP call 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 body The request body object
 * @param headerParams The header parameters
 * @param formParams The form parameters
 * @param authNames The authentications to apply
 * @param progressRequestListener Progress request listener
 * @return The HTTP call
 * @throws ApiException If fail to serialize the request body object
 */
public Call buildCall(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, Object> formParams, String[] authNames, ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
    updateParamsForAuth(authNames, queryParams, headerParams);
    final String url = buildUrl(path, queryParams);
    final Request.Builder reqBuilder = new Request.Builder().url(url);
    processHeaderParams(headerParams, 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);
    }
    Request request = null;
    if (progressRequestListener != null && reqBody != null) {
        ProgressRequestBody progressRequestBody = new ProgressRequestBody(reqBody, progressRequestListener);
        request = reqBuilder.method(method, progressRequestBody).build();
    } else {
        request = reqBuilder.method(method, reqBody).build();
    }
    return httpClient.newCall(request);
}
Also used : Request(com.squareup.okhttp.Request) RequestBody(com.squareup.okhttp.RequestBody)

Example 77 with Request

use of com.squareup.okhttp.Request in project spring-boot-quick by vector4wang.

the class UseHeaders method main.

public static void main(String[] args) throws IOException {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url("http://www.baidu.com").header("User-Agent", "My super agent").addHeader("Accept", "text/html").build();
    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) {
        throw new IOException("服务器端错误: " + response);
    }
    System.out.println(response.header("Server"));
    System.out.println(response.headers("Set-Cookie"));
}
Also used : Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request) IOException(java.io.IOException)

Example 78 with Request

use of com.squareup.okhttp.Request in project openzaly by akaxincom.

the class ZalyHttpClient method postString.

public byte[] postString(String url, String json) throws IOException {
    RequestBody postBody = RequestBody.create(JSON, json);
    Request request = new Request.Builder().url(url).post(postBody).build();
    Response response = httpClient.newCall(request).execute();
    if (response.isSuccessful()) {
        return response.body().bytes();
    } else {
        logger.error("http post error.{}", response.message());
    }
    return null;
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request) RequestBody(com.squareup.okhttp.RequestBody)

Example 79 with Request

use of com.squareup.okhttp.Request in project openzaly by akaxincom.

the class ZalyHttpClient method postBytes.

public byte[] postBytes(String url, byte[] bytes) throws IOException {
    RequestBody postBody = RequestBody.create(JSON, bytes);
    Request request = new Request.Builder().url(url).post(postBody).build();
    Response response = httpClient.newCall(request).execute();
    if (response.isSuccessful()) {
        return response.body().bytes();
    } else {
        logger.error("http post error.{}", response.message());
    }
    return null;
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request) RequestBody(com.squareup.okhttp.RequestBody)

Example 80 with Request

use of com.squareup.okhttp.Request in project openzaly by akaxincom.

the class HttpClient method postKV.

static String postKV(String url) throws IOException {
    RequestBody formBody = new FormEncodingBuilder().add("platform", "android").add("name", "bug").build();
    Request request = new Request.Builder().url(url).post(formBody).build();
    Response response = client.newCall(request).execute();
    System.out.println("post KV response =" + response.isSuccessful());
    if (response.isSuccessful()) {
        return response.body().string();
    } else {
        throw new IOException("Unexpected code " + response);
    }
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request) FormEncodingBuilder(com.squareup.okhttp.FormEncodingBuilder) IOException(java.io.IOException) RequestBody(com.squareup.okhttp.RequestBody)

Aggregations

Request (com.squareup.okhttp.Request)109 Response (com.squareup.okhttp.Response)74 IOException (java.io.IOException)61 OkHttpClient (com.squareup.okhttp.OkHttpClient)38 RequestBody (com.squareup.okhttp.RequestBody)31 FormEncodingBuilder (com.squareup.okhttp.FormEncodingBuilder)19 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 File (java.io.File)11 Callback (com.squareup.okhttp.Callback)10 InputStream (java.io.InputStream)7 Buffer (okio.Buffer)7 HttpUrl (com.squareup.okhttp.HttpUrl)5 MediaType (com.squareup.okhttp.MediaType)5 ResponseBody (com.squareup.okhttp.ResponseBody)5 SocketTimeoutException (java.net.SocketTimeoutException)5 Call (com.squareup.okhttp.Call)4 FileOutputStream (java.io.FileOutputStream)4 Activity (android.app.Activity)3 Intent (android.content.Intent)3 Uri (android.net.Uri)3