Http要求インタフェースHttpClient
6076 ワード
最近restインタフェースを開発して、バックエンドは大量のhttpリクエストがあって、1つのとても実用的なhttpリクエストツールHttpClientを見つけて、もし同時量が高いならばそれとも高いバージョンにアップグレードして、私は4.0.1を使います
pom.xml
HttpUtil.java
テストクラス:
httpUtil注意しなければならないのはリクエスト後、entity.consumeContent()はリソースを閉じる.
pom.xml
4.0.0
com.test.m2
m2-t1
0.0.1-SNAPSHOT
jar
m2-t1
http://maven.apache.org
UTF-8
junit
junit
4.7
test
org.apache.httpcomponents
httpclient
4.0.1
HttpUtil.java
package com.test.m2.t1;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpUtil {
private static HttpClient client = new DefaultHttpClient();
/**
* get
*
* @param url
* @param map
* @return
*/
public static String httpGet(String url, Map map) {
List params = getparamsbyMap(map);
String body = null;
try {
HttpGet get = new HttpGet(url);
String urlParam = EntityUtils.toString(new UrlEncodedFormEntity(
params));
get.setURI(new URI(get.getURI().toString() + "?" + urlParam));
HttpResponse resp = client.execute(get);
HttpEntity entity = resp.getEntity();
body = EntityUtils.toString(entity);
if (entity != null) {
entity.consumeContent(); // , httpclient, , EntityUtils.consume(entity) ;
}
} catch (ParseException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return body;
}
/**
* get
* @param url
* @param map
* @param connectionTimeOut
* @param readTimeOut
* @return
*/
public static String httpGet(String url, Map map,Integer connectionTimeOut,Integer readTimeOut){
setTimeOut(connectionTimeOut, readTimeOut);
return httpGet(url, map);
}
/**
* post
*
* @param url
* @param map
* @return
*/
public static String httpPost(String url, Map map) {
List params = getparamsbyMap(map);
String body = null;
try {
HttpPost post = new HttpPost(url);
post.setEntity(new UrlEncodedFormEntity(params));
HttpResponse resp = client.execute(post);
HttpEntity entity = resp.getEntity();
body = EntityUtils.toString(entity);
if (entity != null) {
entity.consumeContent(); // , httpclient, , EntityUtils.consume(entity) ;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return body;
}
/**
* post
* @param url
* @param map
* @param connectionTimeOut
* @param readTimeOut
* @return
*/
public static String httpPost(String url, Map map,Integer connectionTimeOut,Integer readTimeOut){
setTimeOut(connectionTimeOut, readTimeOut);
return httpPost(url, map);
}
/**
*
* @param map
* @return
*/
private static List getparamsbyMap(Map map) {
List params = new ArrayList();
if (map == null || map.isEmpty()) {
return params;
}
Set keys = map.keySet();
for (String key : keys) {
params.add(new BasicNameValuePair(key, map.get(key).toString()));
}
return params;
}
/**
*
* @param connectionTimeOut
* @param readTimeout
*/
private static void setTimeOut(Integer connectionTimeOut,Integer readTimeout){
client.getParams().setParameter("http.connection.timeout", connectionTimeOut);
client.getParams().setParameter("http.socket.timeout", readTimeout);
}
}
テストクラス:
package com.test.m2.t1;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
public class HttpUtilTest {
@Test
public void testGet(){
String url="http://****:8060/abc/a/b";
Map param = new HashMap();
param.put("a", "SN1002");
param.put("b", " ");
String data = HttpUtil.httpGet(url, param,1000,1000);
System.out.println(data);
}
@Test
public void testPost(){
String url="http://****:8060/abc/a/b";
Map param = new HashMap();
param.put("a", "SN1002");
param.put("b", " ");
String data = HttpUtil.httpGet(url, param,1000,1000);
System.out.println(data);
}
}
httpUtil注意しなければならないのはリクエスト後、entity.consumeContent()はリソースを閉じる.