資源簡介
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
評論
共有 條評論