Example #1
0
        private bool ServerCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors policyErrors)
        {
            if (AllowInvalidCertificates)
            {
                return(true);
            }

            var callback = CertificateValidationCallback;

            HttpCertificate httpCertificate = new HttpCertificate(certificate);

            if (callback == null)
            {
                return(policyErrors == SslPolicyErrors.None && !httpCertificate.IsExpired);
            }
            else
            {
                return(callback(httpCertificate));
            }
        }
 /// <summary>
 /// Compares a HttpCertificate with a HttpCertificate.
 /// </summary>
 /// <param name="other">The HttpCertificate to compare with.</param>
 /// <returns>A Boolean indicating whether these HttpCertificate were equal.</returns>
 public bool Equals(HttpCertificate other)
 {
     return(other != null && CompareBytes(GetRawCertData(), other.GetRawCertData()));
 }
 /// <summary>
 /// Initializes a new HttpResponse using the given HttpWebResponse, the WebExceptionStatus and optionally a HttpCertificate.
 /// </summary>
 /// <param name="response">The HttpWebResponse returned by the http request.</param>
 /// <param name="exception">The Exception.</param>
 /// <param name="exceptionStatus">The WebExceptionStatus returned by the WebRequest.</param>
 /// <param name="certificate">The HttpCertificate used with the http request.</param>
 public HttpResponse(HttpWebResponse response, Exception exception, WebExceptionStatus exceptionStatus, HttpCertificate certificate = null) : this(response, certificate)
 {
     Exception       = exception;
     ExceptionStatus = exceptionStatus;
     Certificate     = certificate;
 }
        /// <summary>
        /// Initializes a new HttpResponse using the given HttpWebResponse and an optional certificate.
        /// </summary>
        /// <param name="response">The HttpWebResponse returned by the http request.</param>
        /// <param name="certificate">The HttpCertificate used with the http request.</param>
        public HttpResponse(HttpWebResponse response, HttpCertificate certificate = null) : this()
        {
            if (response == null)
            {
                throw new ArgumentNullException(nameof(response));
            }

            StatusCode        = response.StatusCode;
            StatusDescription = response.StatusDescription;

            ExceptionStatus = WebExceptionStatus.Success;

            Server      = response.Server;
            ResponseUri = response.ResponseUri;

            ProtocolVersion = response.ProtocolVersion;

            RequestMethod = HttpHelperMethods.RequestMethodFromString(response.Method);

            Certificate = certificate;

            if (response.Headers != null && response.Headers.Count != 0)
            {
                foreach (var key in response.Headers.AllKeys)
                {
                    Headers.Add(new HttpHeader(key, response.Headers[key]));
                }
            }

            if (response.Cookies != null)
            {
                Cookies = response.Cookies;
            }

            LastModified = response.LastModified;

            ContentEncoding = response.ContentEncoding;
            ContentType     = response.ContentType;

            ContentLength = response.ContentLength;

            CharacterSet = response.CharacterSet;

            try
            {
                using (var responseStream = response.GetResponseStream())
                {
                    using (var ms = new MemoryStream())
                    {
                        responseStream.CopyTo(ms);

                        RawBody = ms.ToArray();
                    }
                }

                Body = Encoding.UTF8.GetString(RawBody);
            }
            catch
            {
                Body    = string.Empty;
                RawBody = null;
            }

            response.Dispose();
        }