资源简介
HTTP 请求工具类(含HTTPS)(参数、二进制流、文件、图片)
代码片段和文件信息
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.util.PublicSuffixMatcher;
import org.apache.http.conn.util.PublicSuffixMatcherLoader;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/**
* HTTP 请求工具类
* @author huaieli
*
*/
public class HttpClientUtils {
private static RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(15000)
.setConnectTimeout(15000)
.setConnectionRequestTimeout(15000)
.build();
/**
* 发送HTTP POST请求支持带多个String参数
*
* @param url 链接
* @param paramMap 参数
*/
public static String sendHttpPost(String url Map paramMap) throws Exception {
CloseableHttpClient httpclient = HttpClientUtils.getHttpClient();
return sendHttpPost(url paramMap httpclient);
}
/**
* 发送HTTP POST请求支持多个参数(注:多个参数需拼接)
*
* @param url 链接
* @param params 参数(格式:key1=value1&key2=value2)
*/
public static String sendHttpPost(String url String params) throws Exception {
CloseableHttpClient httpclient = HttpClientUtils.getHttpClient();
return sendHttpPost(url params httpclient);
}
/**
* 发送HTTP POST请求支持带一个文件参数
*
* @param url 链接
* @param file 文件
*/
public static String sendHttpPost(String url File file) throws Exception {
CloseableHttpClient httpclient = HttpClientUtils.getHttpClient();
try {
HttpPost httpPost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file) -1 ContentType.APPLICATION_OCTET_STREAM);
reqEntity.setChunked(true);
httpPost.setEntity(reqEntity);
return sendHttpPost(httpPost httpclient);
} finally {
httpclient.close();
}
}
/**
* 发送HTTP POST请求(客户端采用二进制流发送服务端采用二进制流接收)
*
* @param url 链接
* @param binaryStreamsStr 参数
*/
public sta
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2015-06-29 19:57 HTTP 请求工具类(包含HTTPS)\
文件 727567 2015-05-31 13:10 HTTP 请求工具类(包含HTTPS)\httpclient-4.5.jar
文件 10623 2015-07-03 16:47 HTTP 请求工具类(包含HTTPS)\HttpClientUtils.java
文件 322234 2015-05-01 20:36 HTTP 请求工具类(包含HTTPS)\httpcore-4.4.1.jar
文件 40692 2015-05-31 13:15 HTTP 请求工具类(包含HTTPS)\httpmime-4.5.jar
评论
共有 条评论