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);
}
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"));
}
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;
}
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;
}
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);
}
}
Aggregations