HttpConnection通信
2706 ワード
/**
* URL
*
* @param urlStr
*
* @param contentStr
*
* @param charset
*
* @param sResult
* Buffer
* @return boolean
*/
public boolean sendStrOfPost(String urlStr, String contentStr, String charset, StringBuffer sResult) {
boolean bResult = false;
String charsetName = charset;
URL url = null;
HttpURLConnection httpConnection = null;
InputStream httpIS = null;
java.io.BufferedReader http_reader = null;
try {
url = new URL(urlStr);
httpConnection = (HttpURLConnection) url.openConnection();
// ( : )
httpConnection.setConnectTimeout(Util.getInstance().getIntFromProperties("c1.timeout.httpconn"));
// ( : )
httpConnection.setReadTimeout(Util.getInstance().getIntFromProperties("c1.timeout.httpread"));
httpConnection.setRequestMethod("POST"); // POST
httpConnection.setDoOutput(true);
httpConnection.setRequestProperty("Content-Length", String.valueOf(contentStr.getBytes().length));
PrintWriter out = null;
out = new PrintWriter(new OutputStreamWriter(httpConnection.getOutputStream(), charsetName));//
//
out.print(contentStr);
out.flush();
out.close();
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
//
bResult = true;
//
httpIS = httpConnection.getInputStream();
http_reader = new java.io.BufferedReader(new java.io.InputStreamReader(httpIS, charsetName));
String line = null;
while ((line = http_reader.readLine()) != null) {
if (sResult.length() > 0) {
sResult.append("
");
}
sResult.append(line);
}
logger.info("[URL][response][success]" + sResult);
} else {
logger.info("[URL][response][failure][code : " + responseCode + " ]");
}
} catch (IOException e) {
logger.error("[HttpUtil]sendStrOfPost error", e);
}
finally {
try {
if (http_reader != null)
http_reader.close();
if (httpIS != null)
httpIS.close();
if (httpConnection != null)
httpConnection.disconnect();
} catch (IOException e) {
logger.error("[HttpUtil]finally error", e);
}
}
return bResult;
}