import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.log4j.Logger;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
public class HttpUtil {
private static final Logger logger = Logger.getLogger(HttpUtil.class);
public static String httpPost(String urlStr,JSONObject inParmMap) throws Exception{
String resp= null;
String query = JSONObject.toJSONString(inParmMap,SerializerFeature.WriteMapNullValue);
HttpURLConnection connection = null;
OutputStream outputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
try {
URL url = new URL(urlStr);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setConnectTimeout(120000);
connection.setReadTimeout(120000);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type","application/json");
connection.connect();
outputStream = connection.getOutputStream();
outputStream.write(query.getBytes("UTF-8"));
outputStream.flush();
inputStreamReader = new InputStreamReader(connection.getInputStream());
reader = new BufferedReader(inputStreamReader);
String lines;
StringBuffer sbf = new StringBuffer();
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sbf.append(lines);
}
resp = sbf.toString();
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new Exception(e);
}finally {
if(null != outputStream) outputStream.close();
if(null != inputStreamReader) inputStreamReader.close();
if(null != reader) reader.close();
if(null != connection) connection.disconnect();
}
return resp;
}
}