SetAuthorizationHeader() private static method

private static SetAuthorizationHeader ( HttpHeaders headers, String header, String username, String password ) : void
headers HttpHeaders
header String
username String
password String
return void
Esempio n. 1
0
        public HttpClientResponse TryDispatch(HttpClientRequest request)
        {
            HttpClient.SetAuthorizationHeader(request.Header, "Authorization", this.UserName, this.Password);

            String  lHostname      = request.Url.Hostname;
            Int32   lPort          = request.Url.Port;
            Boolean lSslConnection = String.Equals(request.Url.Protocol, "https", StringComparison.OrdinalIgnoreCase);

            // Settings for connection thru Http Proxy
            // Note that Request should think that it uses direct connection when SSL is enabled because
            // proxy server tunnels SSL data AS IS, without adjusting its HTTP headers
            request.UseProxy = this.ProxySettings.UseProxy && !lSslConnection;
            if (this.ProxySettings.UseProxy)
            {
                lHostname = this.ProxySettings.ProxyHost;
                lPort     = this.ProxySettings.ProxyPort;

                HttpClient.SetAuthorizationHeader(request.Header, "Proxy-Authorization", this.ProxySettings.UserName, this.ProxySettings.Password);
            }

            Connection lConnection = this.GetHttpConnection(lSslConnection, request.Url.Hostname, request.Url.Port, lHostname, lPort);

            try
            {
                request.WriteHeaderToConnection(lConnection);
            }
            catch (ConnectionClosedException)
            {
                lConnection = this.GetNewHttpConnection(lHostname, lPort);
                request.WriteHeaderToConnection(lConnection);
            }
            catch (System.Net.Sockets.SocketException)
            {
                lConnection = this.GetNewHttpConnection(lHostname, lPort);
                request.WriteHeaderToConnection(lConnection);
            }

            request.WriteBodyToConnection(lConnection);

            lConnection.Timeout        = Timeout;
            lConnection.TimeoutEnabled = TimeoutEnabled;

            HttpClientResponse lResponse;

            do
            {
                HttpHeaders lHeaders = HttpHeaders.Create(lConnection);
                if (lHeaders == null)
                {
                    throw new ConnectionClosedException();
                }
                lResponse = new HttpClientResponse(lConnection, lHeaders);
            }while (lResponse.Header.ResponseCode == "100"); // 100 CONTINUE means useless response.

            if (!lResponse.KeepAlive)
            {
                this.fConnectionUrl = null;
                this.fConnection    = null;
            }

            if (lResponse.Code == 407)
            {
                throw new HttpException("Proxy authorization failed", lResponse);
            }

            return(lResponse);
        }
Esempio n. 2
0
        public HttpClientResponse TryDispatch(HttpClientRequest request)
        {
            HttpClient.SetAuthorizationHeader(request.Header, "Authorization", this.UserName, this.Password);

            String  lHostname      = request.URL.Host;
            Int32   lPort          = request.URL.Port;
            Boolean lSslConnection = request.URL.Scheme?.ToLower() == "https";

            // Settings for connection thru Http Proxy
            // Note that Request should think that it uses direct connection when SSL is enabled because
            // proxy server tunnels SSL data AS IS, without adjusting its HTTP headers
            request.UseProxy = this.ProxySettings.UseProxy && !lSslConnection;
            if (this.ProxySettings.UseProxy)
            {
                lHostname = this.ProxySettings.ProxyHost;
                lPort     = this.ProxySettings.ProxyPort;

                HttpClient.SetAuthorizationHeader(request.Header, "Proxy-Authorization", this.ProxySettings.UserName, this.ProxySettings.Password);
            }

            Connection lConnection = this.GetHttpConnection(lSslConnection, request.URL.Host, request.URL.Port, lHostname, lPort);

            try
            {
                request.WriteHeaderToConnection(lConnection);
            }
            catch (ObjectDisposedException)
            {
                lConnection = this.GetNewHttpConnection(lHostname, lPort);
                request.WriteHeaderToConnection(lConnection);
            }
            catch (ConnectionClosedException)
            {
                lConnection = this.GetNewHttpConnection(lHostname, lPort);
                request.WriteHeaderToConnection(lConnection);
            }
            catch (SocketException)
            {
                lConnection = this.GetNewHttpConnection(lHostname, lPort);
                request.WriteHeaderToConnection(lConnection);
            }

            request.WriteBodyToConnection(lConnection);

            HttpClientResponse lResponse;

            do
            {
                HttpHeaders lHeaders = HttpHeaders.Create(lConnection);
                if (lHeaders == null)
                {
                    throw new ConnectionClosedException();
                }
                lResponse = new HttpClientResponse(lConnection, lHeaders);
            }while (lResponse.Header.HttpCode == HttpStatusCode.Continue);            // 100 CONTINUE means useless response.

            if (!lResponse.KeepAlive)
            {
                this.fConnectionUrl = null;
                this.fConnection    = null;
            }

            if (lResponse.HttpCode == HttpStatusCode.ProxyAuthenticationRequired)
            {
                throw new HttpException("Proxy authorization failed", lResponse);
            }

            return(lResponse);
        }