初始化HttpURLConnection
public HttpURLConnection initHttp(String url, String method, Map<String,String> headers)throws IOException {
URL _url =new URL(url);
HttpURLConnection http = (HttpURLConnection) _url.openConnection();
http.setUseCaches(false);
http.setConnectTimeout(5000);
http.setReadTimeout(5000);
http.setRequestMethod(method);
http.setRequestProperty("Accept", "*/*");
http.setRequestProperty("Accept-Encoding", "gzip,deflate");
http.setRequestProperty("Cache-Control", "no-cache");
http.setRequestProperty("Connection", "keep-alive");
if (null != headers && !headers.isEmpty()) {
for (Entry entry : headers.entrySet()) {
http.setRequestProperty(entry.getKey(), entry.getValue());
}
}
return http;
}
GET 请求
public String get(String url, Map<String,Object> params, Map<String,String> headers) {
StringBuffer bufferRes =null;
try {
HttpURLConnection http = initHttp(initParams(url, params), "GET", headers);
int status = http.getResponseCode();
InputStream in =null;
if (status >=400) {
in = http.getErrorStream();
}else {
in = http.getInputStream();
}
BufferedReader read =new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));
String valueString =null;
bufferRes =new StringBuffer();
while ((valueString = read.readLine()) !=null) {
bufferRes.append(valueString);
}
in.close();
if (http !=null) {
http.disconnect();// 关闭连接
}
return bufferRes.toString();
}catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
*初始化参数
*/
public String initParams(String url, Map<String,Object> params) {
if (null == params || params.isEmpty()) {
return url;
}
StringBuilder sb =new StringBuilder(url);
if (url !=null && url.indexOf("?") == -1) {
sb.append("?");
}
sb.append(map2Url(params));
return sb.toString();
}
/**
*map转url参数
*/
public String map2Url (Map<String,Object> paramToMap) {
if (null == paramToMap || paramToMap.isEmpty()) {
return null;
}
StringBuilder url =new StringBuilder();
boolean isFirst =true; //标记一下是否是第一次,第一次不添加&符合
for (Entry entry : paramToMap.entrySet()) {
if (isFirst) {
isFirst =false;
}else {
url.append("&");
}
url.append(entry.getKey()).append("=");
String value =null;
if (entry.getValue() !=null) {
value = String.valueOf(entry.getValue());
}
if (null == value ||"".equals(value.trim())) {
try {
url.append(URLEncoder.encode(value, DEFAULT_CHARSET));
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
url.append(value);
}
return url.toString();
}
POST 请求
public static String post(String url, Map<String,Object> param, Map<String,String> headers) {
StringBuffer bufferRes =null;
try {
HttpURLConnection http =initHttp(url, "POST", headers);
http.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
http.setDoInput(true);
if (params !=null) {
http.setDoOutput(true);
OutputStreamWriter outs =new OutputStreamWriter(
http.getOutputStream(), DEFAULT_CHARSET);
// 发送请求params参数
String params = mapToJson(param)
outs.write(params);
outs.flush();
outs.close();
}
http.connect();
InputStream inputStream =null;
int status = http.getResponseCode();
if (status >=400) {
inputStream = http.getErrorStream();
}else {
inputStream = http.getInputStream();
}
BufferedReader read =new BufferedReader(new InputStreamReader(inputStream, DEFAULT_CHARSET));
String valueString =null;
bufferRes =new StringBuffer();
while ((valueString = read.readLine()) !=null) {
bufferRes.append(valueString);
}
if (inputStream !=null) {
inputStream.close();
}
if (http !=null) {
http.disconnect();// 关闭连接
}
return bufferRes.toString();
}catch (Exception e) {
e.printStackTrace();
return null;
}
}
public String mapToJson(Map map) {
try {
JSONObject object =new JSONObject(map);
return object.toString();
}catch (Exception e) {
return "{}";
}
}