POST로 요청은 하되 쿼리스트링 형식으로 파라미터를 보내지 않고 body에 json형태로 요청을 할 때는 아래와 같이 하면 된다. 음.. 역시나 내 코드는 조잡하다. ㅜㅜ;
HttpClient client = new DefaultHttpClient();
// HttpClient 4.3.x 에서는 new DefaultHttpClient()은 deprecated 되었다고 이클립스에서 친절(?)하게 알려준다. 그냥 가볍게 무시해도 되지만 영 찜찜하다면 아래처럼 하면 된다.
HttpClient client = HttpClientBuilder.create().build();
String url = "http://"+ tipIp +":"+ tipPort +"/get/serverinfo";
HttpPost httpPost = new HttpPost(url);
// 요청데이터를 json형태로 하기 위해 Gson 사용~
Gson gson = new Gson();
String jsonString = gson.toJson(reqCoreServerData);
StringEntity reqEntity = new StringEntity(jsonString);
httpPost.setEntity(reqEntity);
HttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
if( entity != null ) {
InputStream in = null;
try {
in = entity.getContent();
byte[] resultData = new byte[1024]; // 응답데이터의 크기는 최대 1024bytes를 넘지않는다고 가정한다~!!(만약 그렇지 않다면 현재 읽어들인 content의 길이를 확인해서 그만큼 할당해야 함)
int readCnt = in.read(resultData);
System.out.println("\n\n ==== responseData readCnt["+ readCnt +"] data["+ new String(resultData) +"====\n\n");
return null;
}catch(Exception e) {
e.printStackTrace();
return null;
}finally {
if( in != null ) {
in.close();
in = null;
}
if( httpPost != null ) {
httpPost.releaseConnection();
httpPost = null;
}
}
}