/// <summary> /// Initializes a new instance of <see cref="ResponseErrorEventArgs"/> with the specified parameters. /// </summary> /// <param name="requestUri">The request uri.</param> /// <param name="exception">The exception of <see cref="AMicroblogException"/> type.</param> /// <param name="httpMethod">The request method.</param> /// <param name="contentType">The request content type.</param> public ResponseErrorEventArgs(string requestUri, AMicroblogException exception, string httpMethod, string contentType) { ErrorData = new ResponseErrorData(); ErrorData.ErrorCode = exception.ErrorCode; ErrorData.RequestUri = requestUri; ErrorData.Exception = exception; ErrorData.HttpMethod = httpMethod; ErrorData.ContentType = contentType; }
/// <summary> /// Performs the HTTP request. /// </summary> /// <returns>The server response string(UTF8 decoded).</returns> public virtual string Request() { HttpWebRequest req = HttpWebRequest.Create(ConstructUri()) as HttpWebRequest; AppendHeaders(req.Headers); if (!string.IsNullOrEmpty(Method)) { req.Method = Method; } if (!string.IsNullOrEmpty(ContentType)) { req.ContentType = ContentType; } PrepareRequest(req); // Calls GetRequestStream with a "GET" method, an exception is thrown "Cannot send a content-body with this verb-type." if (req.Method == HttpMethod.Post) { using (var reqStream = req.GetRequestStream()) { WriteBody(reqStream); } // ContentLength is automatically calculated. } WebResponse resp = null; try { resp = req.GetResponse(); } catch (WebException wex) { ErrorResponse errorResp; var webResp = wex.Response as HttpWebResponse; if (null == webResp) throw new AMicroblogException(LocalErrorCode.NetworkUnavailable); try { var responseContent = RetrieveResponse(webResp); if (!string.IsNullOrEmpty(responseContent) && responseContent.StartsWith(Constants.XmlHeader, StringComparison.InvariantCultureIgnoreCase)) { errorResp = XmlSerializationHelper.XmlToObject<ErrorResponse>(responseContent); // Retrieves the error code and wraps the exception. var errorCode = AMicroblogException.RetrieveErrorCode(errorResp.ErrorMessage); if (!string.IsNullOrEmpty(errorCode)) errorResp.ErrorCode = int.Parse(errorCode); } else { // Handle other cases errorResp = new ErrorResponse() { ErrorCode = (int)webResp.StatusCode, ErrorMessage = webResp.StatusDescription }; } } catch (Exception exx) { throw new AMicroblogException(LocalErrorCode.ProcessResponseErrorHandlingFailed, exx); } var aex = new AMicroblogException(errorResp.ErrorMessage, wex) { IsRemoteError = true, ErrorCode = errorResp.ErrorCode }; var e = new ResponseErrorEventArgs(req.RequestUri.AbsoluteUri, aex, req.Method, req.ContentType); Environment.NotifyResponseError(e); aex.IsHandled = e.IsHandled; throw aex; } return RetrieveResponse(resp); }