com.github.jackpanz.spring.util.HttpUtil Maven / Gradle / Ivy
The newest version!
package com.github.jackpanz.spring.util;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Administrator on 2016/4/14.
*/
public class HttpUtil {
private static Logger logger = LoggerFactory.getLogger(HttpUtil.class);
private static int connectTimeout = 10000;
private static int readTimeout = 10000;
private static Map requestPropertys = new HashMap() {
{
this.put("Content-type", "application/x-java-serialized-object;charset=utf-8");
}
};
public static String doGet(String urlStr) {
return http("get", urlStr, null, null, connectTimeout, readTimeout);
}
public static String doGet(String urlStr, int connectTimeout, int readTimeout) {
return http("get", urlStr, null, null, connectTimeout, readTimeout);
}
public static String doPost(String urlStr, String parameters) {
return http("post", urlStr, parameters, null, connectTimeout, readTimeout);
}
public static String doPost(String urlStr, String parameters, Map requestPropertys) {
return http("post", urlStr, parameters, null, connectTimeout, readTimeout);
}
public static String doPost(String urlStr, String parameters, Map requestPropertys, int connectTimeout, int readTimeout) {
return http("post", urlStr, parameters, requestPropertys, connectTimeout, readTimeout);
}
public static String http(String method, String urlStr, String parameters) {
return http(method, urlStr, parameters, null, connectTimeout, readTimeout);
}
public static String http(String method, String urlStr, String parameters, Map parmaRequestProperty, int connectTimeout, int readTimeout) {
Map mRequestPropertys = new HashMap();
mRequestPropertys.putAll(requestPropertys);
if (parmaRequestProperty != null) {
mRequestPropertys.putAll(parmaRequestProperty);
}
InputStream inStrm = null;
StringBuffer sb = new StringBuffer();
OutputStreamWriter outputStreamWriter = null;
try {
URL url = new URL(urlStr);
URLConnection rulConnection = url.openConnection();
HttpURLConnection httpUrlConnection = (HttpURLConnection) rulConnection;
for (Map.Entry entry : mRequestPropertys.entrySet()) {
httpUrlConnection.setRequestProperty(entry.getKey(), entry.getValue());
}
httpUrlConnection.setRequestMethod(method.toUpperCase());
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setDoInput(true);
// 設置主機連接超時
httpUrlConnection.setConnectTimeout(connectTimeout);
// 設置從主機讀取數據超時
httpUrlConnection.setReadTimeout(readTimeout);
httpUrlConnection.connect();
if (parameters != null) {
outputStreamWriter = new OutputStreamWriter(httpUrlConnection.getOutputStream(), "utf-8");
outputStreamWriter.write(parameters);
outputStreamWriter.flush();
outputStreamWriter.close();
}
inStrm = rulConnection.getInputStream();
byte[] b = new byte[40960];
for (int n; (n = inStrm.read(b)) != -1; ) {
sb.append(new String(b, 0, n, "utf-8"));
}
} catch (IOException e) {
logger.error(e.getMessage());
return null;
} finally {
IOUtils.closeQuietly(outputStreamWriter);
IOUtils.closeQuietly(inStrm);
}
return sb.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy