com.aliyun.utils.HttpUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of toolkit-maven-plugin
Show all versions of toolkit-maven-plugin
Aliyun Open API SDK for Java
Copyright (C) Alibaba Cloud Computing
All rights reserved.
版权所有 (C)阿里云计算有限公司
http://www.aliyun.com
package com.aliyun.utils;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import java.io.Closeable;
public class HttpUtils {
/**
* POST文本数据
* @param url:目的地址
* @param body:body数据
* @param defaultTimeout: 默认超时,单位毫秒
*/
public static void postPlainText(String url, String body, int defaultTimeout) {
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
try {
//client
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(defaultTimeout)
.setConnectionRequestTimeout(defaultTimeout)
.setSocketTimeout(defaultTimeout).build();
client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
//http post
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(body);
httpPost.setHeader("Content-type", "text/plain");
httpPost.setEntity(entity);
//execute
response = client.execute(httpPost);
} catch (Throwable t) {
//do nothing
} finally {
close(response);
close(client);
}
}
private static void close(Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (Throwable t) {
//do nothing
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy