/**
* 以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("\n");
}
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;
}