Jetty HttpClient Use Case

2014/07/10 14:12
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);
}

Trackback

Trackback Address :: http://www.yongbi.net/trackback/600

Comments

What's on your mind?

댓글 입력 폼
[로그인][오픈아이디란?]
오픈아이디로만 댓글을 남길 수 있습니다