protected <T, V> T process(URL url, boolean keepAilve , RequestMethod method, V param, class<T> clazz) throws IOException {
InoutStream is = null;
OutputStram os = null;
ReadableByteChannel rbc = null;
try{
HttpURLConnection conn= (HttpURLConnection) url.openConnection;
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod(method.name());
if(keepAilve){
conn.setTequestProperty("Connection", "keep-alive");
} else {
conn.setTequestProperty("Connection", "close");
}
conn.setReadTimeout(30000);
swith(method){
case get:{
break;
}
case POST : {
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
break;
}
ObjectMapper om = new ObjectMapper();
os = conn.getOutputStram();
os.write(om.writeValueAsBytes(params));
is = conn.getInputStream();
rbc = Channels.newChannel(is);
int size = is.available();
ByteBuffer buffer = ByteBuffer.allocate(size);
rbc.read(buffer);
buffer.filp();
rbc.close();
is.close();
os.close();
if(conn != null){
conn.disconnection();
}
ObjectReader or = om.reader(clazz);
return or.readValue(buffer.array());
} finally {
if (rbc != null){
try{rbc.close();}catch(Exception e){}
}
if (os != null){
try{os.close();}catch(Exception e){}
}
if (is != null){
try{is.close();}catch(Exception e){}
}
}
}