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); } }
private void handleErrorWebResponse(HttpWebResponse pWebResp, CompletionHandler pCompletionHandler, Exception pExp) { DateTime startTime = DateTime.Now; int statusCode = ResponseInfo.NetworkError; //parse self defined code from the error message string expMsg = pExp.Message; int indexStart = expMsg.IndexOf("("); if (indexStart != -1) { int indexEnd = expMsg.IndexOf(")", indexStart); if (indexStart != -1 && indexEnd != -1) { string statusCodeStr = expMsg.Substring(indexStart + 1, indexEnd - indexStart - 1); try { statusCode = Convert.ToInt32(statusCodeStr); } catch (Exception) { } } } //check for exception string reqId = null; string xlog = null; string ip = null; string xvia = null; string error = null; string host = null; string respData = null; if (pWebResp != null && 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) { } } else { error = pExp.Message; } 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); } }
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; int contentLength = -1; bool recvInvalid = false; if (pWebResp != 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(); } else if (headerName.Equals("Content-Length")) { contentLength = int.Parse(respHeaders[headerName].ToString()); } } } using (StreamReader respStream = new StreamReader(pWebResp.GetResponseStream())) { respData = respStream.ReadToEnd(); if (contentLength > 0) { if (respData.Length != contentLength) { recvInvalid = true; } } } 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) { } if (recvInvalid) { statusCode = -1; string err = string.Format("response-recv is not complete RECV={0},TOTAL={1} {2}", respData.Length, contentLength, error); Console.WriteLine(err); error = err; } pWebResp.Close(); } else { error = "invalid response"; statusCode = -1; } 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); } }