Esempio n. 1
0
        private HttpWebResponse TryDigestRequest(ConnectionOptions co, WebException ex)
        {
            HttpWebResponse response = null;

            try
            {
                var wwwAuthenticateHeader = ex.Response.Headers["WWW-Authenticate"];
                if (wwwAuthenticateHeader == null)
                {
                    return(null);
                }
                if (wwwAuthenticateHeader.StartsWith("Basic", true, CultureInfo.InvariantCulture))
                {
                    return(null); //already failed
                }
                var realm = GrabHeaderVar("realm", wwwAuthenticateHeader);
                var nonce = GrabHeaderVar("nonce", wwwAuthenticateHeader);
                var qop   = GrabHeaderVar("qop", wwwAuthenticateHeader);

                string cnonce = new Random().Next(123400, 9999999).ToString(CultureInfo.InvariantCulture);

                var request = GetRequest(co);

                string authorization = GetDigestHeader(co.source, co.username, co.password, realm, nonce, cnonce, qop);
                request.Headers["Authorization"] = authorization;

                response = (HttpWebResponse)request.GetResponse();
                Uri uri;
                if (Uri.TryCreate(co.source, UriKind.Absolute, out uri))
                {
                    if (uri != null)
                    {
                        AddDigest(new DigestConfig(uri.Host, authorization));
                    }
                }
                co.ExecuteCallback(true);
            }
            catch (ApplicationException)
            {
                //headers missing for digest
                response?.Close();
                response = null;
                co.ExecuteCallback(false);
            }
            catch (Exception ex2)
            {
                Logger.LogException(ex2, "Digest");
                response?.Close();
                response = null;
                co.ExecuteCallback(false);
            }
            return(response);
        }
Esempio n. 2
0
        public void BeginGetResponse(ConnectionOptions co, EventHandler successCallback)
        {
            var request = GetRequest(co);

            co.callback += successCallback;


            var myRequestState = new RequestState {
                Request = request, ConnectionOptions = co
            };

            try
            {
                if (co.data != null && co.data.Length > 0)
                {
                    using (var stream = request.GetRequestStream())
                    {
                        stream.Write(co.data, 0, co.data.Length);
                    }
                }
                request.BeginGetResponse(FinishRequest, myRequestState);
            }
            catch (Exception ex)
            {
                co.ExecuteCallback(false);
                Logger.LogException(ex, "Connection Factory");
            }
        }
Esempio n. 3
0
        private static HttpWebResponse TryDigestRequest(ConnectionOptions co, WebException ex)
        {
            HttpWebResponse response;
            try
            {
                var wwwAuthenticateHeader = ex.Response.Headers["WWW-Authenticate"];
                if (wwwAuthenticateHeader == null)
                    return null;
                if (wwwAuthenticateHeader.StartsWith("Basic", true, CultureInfo.InvariantCulture))
                    return null; //already failed

                var realm = GrabHeaderVar("realm", wwwAuthenticateHeader);
                var nonce = GrabHeaderVar("nonce", wwwAuthenticateHeader);
                var qop = GrabHeaderVar("qop", wwwAuthenticateHeader);

                string cnonce = new Random().Next(123400, 9999999).ToString(CultureInfo.InvariantCulture);

                var request = GetRequest(co);

                string authorization = GetDigestHeader(co.source, co.username, co.password, realm, nonce, cnonce, qop);
                request.Headers["Authorization"] = authorization;

                response = (HttpWebResponse)request.GetResponse();
                Uri uri;
                if (Uri.TryCreate(co.source, UriKind.Absolute, out uri))
                {
                    if (uri != null)
                    {
                        AddDigest(new DigestConfig(uri.Host, authorization));
                    }
                }
                co.ExecuteCallback(true);
            }
            catch (ApplicationException)
            {
                //headers missing for digest
                response = null;
                co.ExecuteCallback(false);
            }
            catch (Exception ex2)
            {
                Logger.LogExceptionToFile(ex2, "Digest");
                response = null;
                co.ExecuteCallback(false);
            }
            return response;
        }
Esempio n. 4
0
        public static void BeginGetResponse(ConnectionOptions co, EventHandler successCallback)
        {
            var request = GetRequest(co);
            co.callback += successCallback;


            var myRequestState = new RequestState { Request = request, ConnectionOptions = co };
            try
            {
                if (co.data != null)
                {
                    using (var stream = request.GetRequestStream())
                    {
                        stream.Write(co.data, 0, co.data.Length);
                    }
                }
                request.BeginGetResponse(FinishRequest, myRequestState);
            }
            catch (Exception ex)
            {
                co.ExecuteCallback(false);
                Logger.LogExceptionToFile(ex, "Connection Factory");
            }

            
        }