Jetty HttpClient를 이용하여 HTTP 호출
try {
//Synchronous Request
HttpClient httpClient = new HttpClient();
//set up httpClient
httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
httpClient.start();
//Request-Response Exchange
ContentExchange contentExchange = new ContentExchange();
contentExchange.setMethod(method);
contentExchange.setURL(url);
//Request Header
if (keyApply) {
if (baseApply)
contentExchange.addRequestHeader("Authorization", "Basic " + authBase64Encode);
else
contentExchange.addRequestHeader("Authorization", key);
}
//Request Body
contentExchange.addRequestHeader("Content-Type", contentType);
if (data == null || data.length == 0) {
contentExchange.addRequestHeader("Content-Length", "0");
resultData.append("Content-Length : 0\n");
}
else {
contentExchange.addRequestHeader("Content-Length", String.valueOf(data.length));
resultData.append("Content-Length : " + String.valueOf(data.length) + "\n");
contentExchange.setRequestContentSource(new ByteArrayInputStream(data));
}
httpClient.send(contentExchange);
//Wait for Response Done
int exchangeStatus = contentExchange.waitForDone();
//Response Data
int responseStatus = contentExchange.getResponseStatus();
resultData.append("Response Status : " + responseStatus + "\n");
if (exchangeStatus == HttpExchange.STATUS_COMPLETED) {
//Response Data
String response = contentExchange.getResponseContent();
if (response != null && !response.isEmpty())
resultData.append("Response Data : \n" + response + "\n");
else
resultData.append("Response Data is null.\n");
}
} catch (Exception e1) {
e1.printStackTrace();
resultData.setText(e1.getMessage());
//throw new ServletException(e1);
}
댓글을 달아 주세요
댓글 RSS 주소 : http://www.yongbi.net/rss/comment/600