import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import com.huawei.bme.commons.om.log.DebugLog;
import com.huawei.bme.commons.om.log.LogFactory;
import com.huawei.iread.util.DateUtil;
{
// 響應碼
int sc = HttpURLConnection.HTTP_OK;
// 記錄入口日誌,並加上時戳,以方便定位出其它系統無響應或響應慢方面的問題
if (log.isInfoEnable())
{
log.info("begin sendHttp()..., request=" + request.toString() + ",requesurl =" + request.getUrl() + " ,to="
+ request.getTo() + ", response=" + response.toString() + ", timestamp=" + DateUtil.getCurrentTime());
}
final long start = System.currentTimeMillis();
long delay = 0;
InputStream in = null;
OutputStream out = null;
HttpURLConnection connect = null;
ByteArrayOutputStream rspBuf = null;
try
{
// 打開連接
connect = (HttpURLConnection)request.getUrl().openConnection();
// 設置超時時間
int timeout = request.getConnectTimeout();
if (0 <= timeout)
{
connect.setConnectTimeout(timeout);
}
connect.setDoInput(true); // 打開輸入流(讀取 服務器的輸出)
connect.setDoOutput(true); // 打開輸出流(將請求 寫到服務器)
// 設置 method
if (null != request.getRequestMethod())
{
connect.setRequestMethod(request.getRequestMethod());
}
// 設置 header
String[][] headers = request.getAllHeaders();
for (int i = 0; i < headers.length; i++)
{
connect.setRequestProperty(headers[i][0], headers[i][1]);
}
// 把請求 輸出到服務器
out = connect.getOutputStream();
out.write(request.getReqBody());
out.flush();
// 讀取服務器的響應
sc = connect.getResponseCode();
response.setResponseCode(sc);
if (HttpURLConnection.HTTP_OK == sc)
{
// 設置響應編碼
String enc = connect.getContentEncoding();
if (null == enc)
{
String cntType = connect.getContentType();
if (null != cntType)
{
cntType = cntType.toLowerCase();
int idx = cntType.lastIndexOf("charset=");
if (0 < idx && (idx + 8) < cntType.length())
{
enc = cntType.substring(idx + 8).trim();
}
}
}
response.setCharset(enc);
// 讀取響應
in = connect.getInputStream();
// rspBuf = new BufferedInputStream(in);
rspBuf = new ByteArrayOutputStream(1024 * 8);
byte[] byteBuf = new byte[1024];
int len = 0;
while (0 < (len = in.read(byteBuf)))
{
rspBuf.write(byteBuf, 0, len);
}
delay = System.currentTimeMillis() - start;
// 解析響應
response.parseResBody(new java.io.ByteArrayInputStream(rspBuf.toByteArray()));
}
}
catch (Exception e)
{
sc = HttpURLConnection.HTTP_NOT_FOUND;
delay = System.currentTimeMillis() - start;
log.error("send http request failed", e);
}
finally
{
if (null != in)
{
try
{
in.close();
}
catch (IOException e)
{
log.error("close HTTP InputStream failed", e);
}
}
if (null != out)
{
try
{
out.close();
}
catch (IOException e)
{
log.error("close HTTP OutStream failed", e);
}
}
}
// 記錄出口日誌,並加上時戳,以方便定位出其它系統無響應或響應慢方面的問題
String rspBody = null;
if (log.isDebugEnable())
{
// 詳細響應信息(爲了性能考慮,只有日誌爲debug級別時纔打印)
String charset = response.getCharset();
charset = ((null == charset) ? HttpReq.CHARSET_UTF8 : charset);
if (null != rspBuf)
{
rspBody = ", rspBody=";
try
{
rspBody += new String(rspBuf.toByteArray(), charset);
}
catch (UnsupportedEncodingException e)
{
log.error("parse response body failed", e);
}
}
}
if (log.isInfoEnable())
{
log.info("end sendHttp(), request=" + request.toString() + ", response=" + response.toString() + ", sc="
+ sc + ", timestamp=" + DateUtil.getCurrentTime() + ", delay=" + delay + "(ms)"
+ (null != rspBody ? rspBody.toLowerCase() : ""));
}
return sc;
}
java.net.HttpURLConnection;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.