1. 概念 httpClient区分
HttpComponent 是 apache-commons区别:在apache的官网上下载httpclient的jar包时,会发现它是在一个叫HttpComponent的项目下,这个HttpComponent是apache的顶级项目。而以前的commons的那三个包都是commons的项目下,原来,commons-httpclient 是 apache-commons 项目下的一个子项目,后来被 HttpComponents 取代.
2. HttpComponents 各个版本使用方法
2.使用流程:
- 创建HttpClient对象。
- 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
- 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。
- 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。
- 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
- 释放连接。无论执行方法是否成功,都必须释放连接
httpclient3.x
HttpClient client = new HttpClient();
// 设置代理服务器地址和端口
// client.getHostConfiguration().setProxy("proxy_host_addr",proxy_port);
// 使用 GET 方法 ,如果服务器需要通过 HTTPS 连接,那只需要将下面 URL 中的 http 换成 https
HttpMethodmethod = new GetMethod("http://java.sun.com");
// 使用POST方法
// HttpMethod method = new PostMethod("http://java.sun.com");
client.executeMethod(method);
// 打印服务器返回的状态
System.out.println(method.getStatusLine());
// 打印返回的信息
System.out.println(method.getResponseBodyAsString());
// 释放连接
method.releaseConnection();
httpclient4.x到httpclient4.3以下
public void getUrl(String url, String encoding) throws ClientProtocolException, IOException {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(instream, encoding));
System.out.println(reader.readLine());
} catch (Exception e) {
e.printStackTrace();
} finally {
instream.close();
}
}
// 关闭连接.
client.getConnectionManager().shutdown();
httpclient4.3以上
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public static String getResult(String urlStr) {
CloseableHttpClient httpClient = HttpClients.createDefault();
// HTTP Get请求
HttpGet httpGet = new HttpGet(urlStr);
// 设置请求和传输超时时间
// RequestConfig requestConfig =
// RequestConfig.custom().setSocketTimeout(TIME_OUT).setConnectTimeout(TIME_OUT).build();
// httpGet.setConfig(requestConfig);
String res = "";
try {
// 执行请求
HttpResponse getAddrResp = httpClient.execute(httpGet);
HttpEntity entity = getAddrResp.getEntity();
if (entity != null) {
res = EntityUtils.toString(entity);
}
log.info("响应" + getAddrResp.getStatusLine());
} catch (Exception e) {
log.error(e.getMessage(), e);
return res;
} finally {
try {
httpClient.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
return res;
}
}
return res;
}
3.commons-httpclient 用法(线程安全)
//改完后多线程请求方式
connectionManager = new MultiThreadedHttpConnectionManager();
HttpConnectionManagerParams params = connectionManager.getParams();
params.setConnectionTimeout(60000);
params.setSoTimeout(60000);
params.setDefaultMaxConnectionsPerHost(32);
httpClient = new HttpClient(connectionManager);
httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
//设置http版本协议
httpClient.getParams().setVersion(HttpVersion.HTTP_1_0);
//现在使用的请求方式
//HttpClientParams httpClientParams = new HttpClientParams();
//httpClientParams.setVersion(HttpVersion.HTTP_1_0);
/*if (httpClient == null) {
httpClient = new HttpClient(httpClientParams);
}*/
//httpClient.getHostConfiguration().setProxy("127.0.0.1", 8888);
/*httpClient.getHttpConnectionManager().getParams()
.setConnectionTimeout(60000);
httpClient.getHttpConnectionManager().getParams().setSoTimeout(60000);*/
//在使用httpClient客户端工具时,需要设置标红颜色的属性,否则就会出现Connection is not open 这个异
httpClient.getHttpConnectionManager().getParams().setDefaultMaxConnectionsPerHost(32);
httpClient.getHttpConnectionManager().getParams().setMaxTotalConnections(256);
logger.info("HTTP 发送请求开始");
PostMethodUtil tProcPost = new PostMethodUtil(cUrlStr);
tProcPost.setRequestHeader("Connection", "close");
Set tKeySet = cParamMap.keySet();
for (Iterator i$ = tKeySet.iterator(); i$.hasNext();) {
Object tKey = i$.next();
String tKeyValue = String.valueOf(tKey);
String tValue = (String) cParamMap.get(tKeyValue);
if (tValue == null) {
tValue = "";
}
tProcPost.addParameter(tKeyValue, tValue);
}
logger.info("HTTP 请求开始");
int tStatusCode = httpClient.executeMethod(tProcPost);
logger.info(new StringBuilder().append("HTTP 返回状态码:")
.append(tStatusCode).toString());
InputStream tResInputStream = null;
StringBuffer tHtml = new StringBuffer();
try {
tResInputStream = tProcPost.getResponseBodyAsStream();
BufferedReader tReader = new BufferedReader(new InputStreamReader(
tResInputStream, cCharsetType));
String tTempBf = null;
while ((tTempBf = tReader.readLine()) != null) {
tHtml.append(tTempBf);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (tResInputStream != null) {
tResInputStream.close();
}
if (tProcPost != null) {
tProcPost.releaseConnection();
}
}
logger.info("HTTP 请求结束");
logger.info("http请求消息头=" + tProcPost.getRequestHeaders());
return new String(tHtml.toString().getBytes(cCharsetType), cCharsetType);
4. HttpComponent 用法(线程安全)
public static String httpPostMap(String Url, Map<String,String> map){
//创建线程安全的链接管理
PoolingHttpClientConnectionManager poolConnectionManager = new PoolingHttpClientConnectionManager();
poolConnectionManager.setMaxTotal(1000);
//使用httpClient创建实例 CloseableHttpClient
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(poolConnectionManager)
.build();
//获取httpClient连接实例
//CloseableHttpClient httpClient= HttpClients.createDefault();
//初始化请求方式
HttpPost httpPost=new HttpPost(Url);
//设置协议号
httpPost.setProtocolVersion(HttpVersion.HTTP_1_0);
//设置参数发送
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
for(Map.Entry<String,String> entry : map.entrySet()) {
pairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
}
//转换参数并设置编码格式
httpPost.setEntity(new UrlEncodedFormEntity(pairs, Consts.UTF_8));
//设置超时间
RequestConfig requestConfig=RequestConfig.custom().setSocketTimeout(6000).setConnectTimeout(60000).build();
httpPost.setConfig(requestConfig);
String res="";
CloseableHttpResponse resp=null;
try {
//开始请求
resp=httpClient.execute(httpPost);
HttpEntity httpEntity= resp.getEntity();
if(httpEntity!=null){
res= EntityUtils.toString(httpEntity);
System.out.println("响应码:"+resp.getStatusLine()+" 响应信息:"+res);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//释放连接
try {
if(resp!=null){
resp.close();
}
if(httpPost!=null){
httpPost.releaseConnection();
}
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return res;
}
4. Content-Type
Http Header里的Content-Type一般有这三种:
application/x-www-form-urlencoded:数据被编码为名称/值对。这是标准的编码格式。
multipart/form-data: 数据被编码为一条消息,页上的每个控件对应消息中的一个部分。
text/plain: 数据以纯文本形式(text/json/xml/html)进行编码,其中不含任何控件或格式字符。postman软件里标的是RAW。
form的enctype属性为编码方式,常用有两种:application/x-www-form-urlencoded和multipart/form-data,默认为application/x-www-form-urlencoded。
当action为get时候,浏览器用x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1&name2=value2…),然后把这个字串追加到url后面,用?分割,加载这个新的url。
当action为post时候,浏览器把form数据封装到http body中,然后发送到server。 如果没有type=file的控件,用默认的application/x-www-form-urlencoded就可以了。 但是如果有type=file的话,就要用到multipart/form-data了。
当action为post且Content-Type类型是multipart/form-data,浏览器会把整个表单以控件为单位分割,并为每个部分加上Content-Disposition(form-data或者file),Content-Type(默认为text/plain),name(控件name)等信息,并加上分割符(boundary)。