private void handleWebResponse(HttpWebResponse pWebResp, CompletionHandler pCompletionHandler)
{
DateTime startTime = DateTime.Now;
//check for exception
int statusCode = ResponseInfo.NetworkError;
string reqId = null;
string xlog = null;
string ip = null;
string xvia = null;
string error = null;
string host = null;
string respData = null;
statusCode = (int)pWebResp.StatusCode;
if (pWebResp.Headers != null)
{
WebHeaderCollection respHeaders = pWebResp.Headers;
foreach (string headerName in respHeaders.AllKeys)
{
if (headerName.Equals("X-Reqid"))
{
reqId = respHeaders[headerName].ToString();
}
else if (headerName.Equals("X-Log"))
{
xlog = respHeaders[headerName].ToString();
}
else if (headerName.Equals("X-Via"))
{
xvia = respHeaders[headerName].ToString();
}
else if (headerName.Equals("X-Px"))
{
xvia = respHeaders[headerName].ToString();
}
else if (headerName.Equals("Fw-Via"))
{
xvia = respHeaders[headerName].ToString();
}
else if (headerName.Equals("Host"))
{
host = respHeaders[headerName].ToString();
}
}
using (StreamReader respStream = new StreamReader(pWebResp.GetResponseStream()))
{
respData = respStream.ReadToEnd();
}
try
{
/////////////////////////////////////////////////////////////
// 改进Response的error解析, 根据HttpStatusCode
// @fengyh 2016-08-17 18:29
/////////////////////////////////////////////////////////////
if (statusCode != (int)HCODE.OK)
{
bool isOtherCode = HttpCode.GetErrorMessage(statusCode, out error);
if (isOtherCode)
{
Dictionary <string, string> errorDict = JsonConvert.DeserializeObject <Dictionary <string, string> >(respData);
error = errorDict["error"];
}
}
}
catch (Exception) { }
}
double duration = DateTime.Now.Subtract(startTime).TotalSeconds;
ResponseInfo respInfo = new ResponseInfo(statusCode, reqId, xlog, xvia, host, ip, duration, error);
if (pCompletionHandler != null)
{
pCompletionHandler(respInfo, respData);
}
}