示例#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="PersyConnectionException">Thrown upon connection failure.</exception>
        /// <exception cref="PersyErrorResponseException">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 PersyConnectionException(String.Format("Could not read response from Persephony 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 PersyConnectionException(String.Format("Could not read response from Persephony API: {0}", e.Message));
                }
            }
            else
            {
                PersyError error = new PersyError(500, "No response could be retrieved from Persephony", -1, String.Empty);
                if (response != null)
                {
                    error = new PersyError((int)response.StatusCode, response.StatusDescription ?? response.StatusCode.ToString("G"), -1, String.Empty);
                }
                throw new PersyErrorResponseException(error);
            }
        }
 /// <summary>
 /// Initializes a new instance of the PersyErrorResponseException class with a specified string contents.
 /// </summary>
 /// <param name="pe">PersyError object.</param>
 /// <see cref="DateTime">PersyError method.</see>
 public PersyErrorResponseException(PersyError pe) : base(String.Format("ErrorResponse ({0})::{1}:{2}", pe.getStatus, pe.getCode, pe.getMessage))
 {
     this.pe = pe;
 }
 /// <summary>
 /// Initializes a new instance of the PersyErrorResponseException 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 PersyErrorResponseException(int status, String message, int code, String info) : base(String.Format("ErrorResponse ({0})::{1}:{2}", status, code, message))
 {
     this.pe = new PersyError(status, message, code, info);
 }