Jetty HttpClient Use Case

Articles 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);
}

받은 트랙백이 없고, 댓글이 없습니다.

댓글+트랙백 RSS :: http://www.yongbi.net/rss/response/600

트랙백 주소 :: http://www.yongbi.net/trackback/600

트랙백 RSS :: http://www.yongbi.net/rss/trackback/600

댓글을 달아 주세요

댓글 RSS 주소 : http://www.yongbi.net/rss/comment/600
[로그인][오픈아이디란?]
오픈아이디로만 댓글을 남길 수 있습니다