Пример #1
0
        /// <summary>
        ///	Read the response to a HTTP request.
        /// </summary>
        /// <param name="request">HTTP requestion.</param>
        /// <returns>String containing the response body of the request.</returns>
        /// <exception cref="FreeClimbConnectionException">Thrown upon connection failure.</exception>
        /// <exception cref="FreeClimbErrorResponseException">Thrown upon unsuccessful HTTP request.</exception>
        private string readResponse(HttpWebRequest request)
        {
            HttpWebResponse response;

            try
            {
                response = request.GetResponse() as HttpWebResponse;
            }
            catch (WebException e)
            {
                response = e.Response as HttpWebResponse;
            }
            catch (Exception e)
            {
                throw new FreeClimbConnectionException(String.Format("Could not read response from FreeClimb API: {0}", e.Message));
            }

            if ((response != null) &&
                ((int)response.StatusCode >= 200) &&
                ((int)response.StatusCode < 300))
            {
                try
                {
                    using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
                    {
                        return(streamReader.ReadToEnd());
                    }
                }
                catch (Exception e)
                {
                    throw new FreeClimbConnectionException(String.Format("Could not read response from FreeClimb API: {0}", e.Message));
                }
            }
            else
            {
                FreeClimbError error = new FreeClimbError(500, "No response could be retrieved from FreeClimb", -1, String.Empty);
                if (response != null)
                {
                    error = new FreeClimbError((int)response.StatusCode, response.StatusDescription ?? response.StatusCode.ToString("G"), -1, String.Empty);
                }
                throw new FreeClimbErrorResponseException(error);
            }
        }
Пример #2
0
 /// <summary>
 /// Initializes a new instance of the FreeClimbErrorResponseException class with a specified string contents.
 /// </summary>
 /// <param name="pe">FreeClimbError object.</param>
 /// <see cref="DateTime">FreeClimbError method.</see>
 public FreeClimbErrorResponseException(FreeClimbError pe) : base(String.Format("ErrorResponse ({0})::{1}:{2}", pe.getStatus, pe.getCode, pe.getMessage))
 {
     this.pe = pe;
 }
Пример #3
0
 /// <summary>
 /// Initializes a new instance of the FreeClimbErrorResponseException class with a specified string contents.
 /// </summary>
 /// <param name="status">Response status code</param>
 /// <param name="message">Response status description</param>
 /// <param name="code">Code</param>
 /// <param name="info">Message</param>
 public FreeClimbErrorResponseException(int status, String message, int code, String info) : base(String.Format("ErrorResponse ({0})::{1}:{2}", status, code, message))
 {
     this.pe = new FreeClimbError(status, message, code, info);
 }