當前位置: 首頁>>代碼示例>>C#>>正文


C# HttpRequestMessage.GetClientIpAddress方法代碼示例

本文整理匯總了C#中System.Net.Http.HttpRequestMessage.GetClientIpAddress方法的典型用法代碼示例。如果您正苦於以下問題:C# HttpRequestMessage.GetClientIpAddress方法的具體用法?C# HttpRequestMessage.GetClientIpAddress怎麽用?C# HttpRequestMessage.GetClientIpAddress使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Net.Http.HttpRequestMessage的用法示例。


在下文中一共展示了HttpRequestMessage.GetClientIpAddress方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: WebApiRaygunRequestMessage

        public WebApiRaygunRequestMessage(HttpRequestMessage request)
        {
            HostName = request.RequestUri.Host;
            Url = request.RequestUri.AbsolutePath;
            HttpMethod = request.Method.ToString();
            Headers = new Dictionary<string, string>();

            foreach (var header in request.Headers)
            {
                Headers.Add(header.Key, string.Join(",", header.Value));
            }
            IPAddress = request.GetClientIpAddress();

            if (request.Content.Headers.ContentLength.HasValue && request.Content.Headers.ContentLength.Value > 0)
            {

                foreach (var header in request.Content.Headers)
                {
                    Headers.Add(header.Key, string.Join(",", header.Value));
                }

                try
                {
                    RawData = request.Content.ReadAsStringAsync().Result;
                }
                catch (Exception)
                {
                }
            }
        }
開發者ID:WebApiContrib,項目名稱:WebApiContrib.Logging.Raygun,代碼行數:30,代碼來源:WebApiRaygunRequestMessage.cs

示例2: LogRequestLoggingInfo

        private void LogRequestLoggingInfo(HttpRequestMessage request)
        {
            var info = new ApiLoggingInfo
            {
                HttpMethod = request.Method.Method,
                UriAccessed = request.RequestUri.AbsoluteUri,
                IpAddress = request.GetClientIpAddress(),
                MessageType = HttpMessageType.Request,
                LoggingLevel = LoggingLevel.Trace
            };

            ExtractMessageHeadersIntoLoggingInfo(info, request.Headers.ToList());

            if (request.Content != null)
            {
                request.Content.ReadAsByteArrayAsync()
                    .ContinueWith(task =>
                    {
                        info.BodyContent = Encoding.UTF8.GetString(task.Result);
                        _repository.Log(info);

                    });

                return;
            }

            _repository.Log(info);
        }
開發者ID:liumeifu,項目名稱:OSky,代碼行數:28,代碼來源:RequestResponseTraceHandlerHandler.cs

示例3: GetClientIpAddressForEmptyXForwardedForReturnsNull

        public void GetClientIpAddressForEmptyXForwardedForReturnsNull()
        {
            var sut = new HttpRequestMessage();
            sut.Headers.Add(ForwardedFor, string.Empty);

            Assert.Equal(null, sut.GetClientIpAddress());
        }
開發者ID:Georadix,項目名稱:Georadix.NET,代碼行數:7,代碼來源:HttpRequestMessageExtensionsFixture.cs

示例4: GetClientIpAddressForPartiallyMalformedXForwardedForReturnsProperlyFormedClientIp

        public void GetClientIpAddressForPartiallyMalformedXForwardedForReturnsProperlyFormedClientIp()
        {
            var sut = new HttpRequestMessage();
            sut.Headers.Add(ForwardedFor, MalformedIpAddress);
            sut.Headers.Add(ForwardedFor, GoogleIpAddress);
            sut.Headers.Add(ForwardedFor, MalformedIpAddress);

            Assert.Equal(GoogleIpAddress, sut.GetClientIpAddress());
        }
開發者ID:Georadix,項目名稱:Georadix.NET,代碼行數:9,代碼來源:HttpRequestMessageExtensionsFixture.cs

示例5: GetClientIpAddressForPublicClientAndMultipleProxiesReturnsPublicClientIp

        public void GetClientIpAddressForPublicClientAndMultipleProxiesReturnsPublicClientIp()
        {
            var sut = new HttpRequestMessage();
            sut.Headers.Add(ForwardedFor, MicrosoftIpAddress);
            sut.Headers.Add(ForwardedFor, Private16Bit);
            sut.Headers.Add(ForwardedFor, Private20Bit);
            sut.Headers.Add(ForwardedFor, Private24Bit);
            sut.Headers.Add(ForwardedFor, PrivateLinkLocal);

            Assert.Equal(MicrosoftIpAddress, sut.GetClientIpAddress());
        }
開發者ID:Georadix,項目名稱:Georadix.NET,代碼行數:11,代碼來源:HttpRequestMessageExtensionsFixture.cs

示例6: GetUserIdentifier

 protected virtual string GetUserIdentifier(HttpRequestMessage request) {
     HttpRequestContext ctx = request.GetRequestContext();
     if (ctx != null) {
         // use organization id or user id as the identifier for throttling
         var principal = request.GetRequestContext().Principal as ExceptionlessPrincipal;
         if (principal != null)
             return principal.Project != null ? principal.Project.OrganizationId : principal.UserEntity.Id;
     }
     // fallback to using the IP address
     var ip = request.GetClientIpAddress();
     return Settings.Current.WebsiteMode == WebsiteMode.Dev && String.IsNullOrEmpty(ip) ? "127.0.0.1" : ip;
 }
開發者ID:priestd09,項目名稱:Exceptionless,代碼行數:12,代碼來源:ThrottlingHandler.cs

示例7: GetUserIdentifier

        protected virtual string GetUserIdentifier(HttpRequestMessage request) {
            HttpRequestContext ctx = request.GetRequestContext();
            if (ctx != null) {
                // use organization id or user id as the identifier for throttling
                var principal = request.GetRequestContext().Principal as ExceptionlessPrincipal;
                if (principal != null)
                    return principal.Project != null ? principal.Project.OrganizationId : principal.UserEntity.Id;
            }

            // fallback to using the IP address
            return request.GetClientIpAddress();
        }
開發者ID:khoussem,項目名稱:Exceptionless,代碼行數:12,代碼來源:ThrottlingHandler.cs

示例8: TestGetClientIpAddressFromRemoteEndpointMessage

        public void TestGetClientIpAddressFromRemoteEndpointMessage()
        {
            var remoteEndpointMessageProperty = new RemoteEndpointMessageProperty("127.0.0.1", 8050);
            var httpRequestMessage = new HttpRequestMessage();
            httpRequestMessage.Properties.Add(
                "System.ServiceModel.Channels.RemoteEndpointMessageProperty",
                remoteEndpointMessageProperty);

            var result = httpRequestMessage.GetClientIpAddress();

            Assert.That(result, Is.EqualTo("127.0.0.1"));
        }
開發者ID:TerraVenil,項目名稱:WebAPIContrib,代碼行數:12,代碼來源:HttpRequestMessageExtensionsTests.cs

示例9: TestGetClientIpAddressFromOwinContext

        public void TestGetClientIpAddressFromOwinContext()
        {
            var owinContextStub = MockRepository.GenerateMock<OwinContext>();
            var owinRequest = new OwinRequest { RemoteIpAddress = "127.0.0.1" };
            owinContextStub.Stub(x => x.Request).Return(owinRequest).Repeat.Once();
            var httpRequestMessage = new HttpRequestMessage();
            httpRequestMessage.Properties.Add("MS_OwinContext", owinContextStub);

            var result = httpRequestMessage.GetClientIpAddress();

            Assert.That(result, Is.EqualTo("127.0.0.1"));
        }
開發者ID:TerraVenil,項目名稱:WebAPIContrib,代碼行數:12,代碼來源:HttpRequestMessageExtensionsTests.cs

示例10: SendAsync

        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var corrId = string.Format("{0}{1}", DateTime.Now.Ticks, Thread.CurrentThread.ManagedThreadId);
            var requestInfo = string.Format("{0} {1}", request.Method, request.RequestUri);

            var _CurrentDateTime = DateTime.Now;

            var _CurrentIpAddress = request.GetClientIpAddress();

            var requestMessage = await request.Content.ReadAsByteArrayAsync();
            var reqHeaders = "";
            var header = request.Headers;
            if (header != null)
            {
                //for (int i = 0; i < header.Count(); i++)
                //    request += header

                var _aaa = header.ToArray();
                for (int i = 0; i < _aaa.Count(); i++)
                    reqHeaders += _aaa[i].Key + ":" + _aaa[i].Value.FirstOrDefault() + ", ";


            }

            var httpStatusCode = "";

            byte[] requestBody = null;
            if (request.Content != null)
                requestBody = await request.Content.ReadAsByteArrayAsync();


            await IncommingMessageAsync(_CurrentIpAddress, _CurrentDateTime, corrId, requestInfo, requestMessage, reqHeaders, httpStatusCode, requestBody);

            var response = await base.SendAsync(request, cancellationToken);

            httpStatusCode = (int)response.StatusCode + " " + response.StatusCode.ToString();
            byte[] responseMessage;

            //if (response.IsSuccessStatusCode)
            //    responseMessage = await response.Content.ReadAsByteArrayAsync();
            //else
            byte[] _responseMessageMain = null;

            if (response.Content != null)
                _responseMessageMain = await response.Content.ReadAsByteArrayAsync();
            responseMessage = Encoding.UTF8.GetBytes(response.ReasonPhrase);

            await OutgoingMessageAsync(_CurrentIpAddress, _CurrentDateTime, corrId, requestInfo, responseMessage, reqHeaders, httpStatusCode, requestBody, _responseMessageMain);

            return response;
        }
開發者ID:surifoll,項目名稱:git-test,代碼行數:51,代碼來源:MessageHandler.cs

示例11: TestGetClientIpAddressFromHttpContext

        public void TestGetClientIpAddressFromHttpContext()
        {
            var httpContextBaseStub = MockRepository.GenerateMock<HttpContextBase>();
            var httpRequestBaseStub = MockRepository.GenerateMock<HttpRequestBase>();
            httpRequestBaseStub.Stub(x => x.UserHostAddress).Return("127.0.0.1").Repeat.Once();
            httpContextBaseStub.Stub(x => x.Request).Return(httpRequestBaseStub).Repeat.Once();
            var httpRequestMessage = new HttpRequestMessage();
            httpRequestMessage.Properties.Add("MS_HttpContext", httpContextBaseStub);

            var result = httpRequestMessage.GetClientIpAddress();

            Assert.That(result, Is.EqualTo("127.0.0.1"));

            httpContextBaseStub.VerifyAllExpectations();
            httpRequestBaseStub.VerifyAllExpectations();
        }
開發者ID:TerraVenil,項目名稱:WebAPIContrib,代碼行數:16,代碼來源:HttpRequestMessageExtensionsTests.cs

示例12: GetUserIdentifier

        protected virtual string GetUserIdentifier(HttpRequestMessage request) {
            var authType = request.GetAuthType();
            if (authType == AuthType.Token)
                return request.GetDefaultOrganizationId();

            if (authType == AuthType.User) {
                var user = request.GetUser();
                if (user != null)
                    return user.Id;
            }

            // fallback to using the IP address
            var ip = request.GetClientIpAddress();
            if (String.IsNullOrEmpty(ip) || ip == "::1")
                ip = "127.0.0.1";

            return ip;
        }
開發者ID:rpotalara,項目名稱:Exceptionless,代碼行數:18,代碼來源:ThrottlingHandler.cs

示例13: GetClientIpAddressForNullXForwardedForReturnsNull

        public void GetClientIpAddressForNullXForwardedForReturnsNull()
        {
            var sut = new HttpRequestMessage();

            Assert.Equal(null, sut.GetClientIpAddress());
        }
開發者ID:Georadix,項目名稱:Georadix.NET,代碼行數:6,代碼來源:HttpRequestMessageExtensionsFixture.cs

示例14: GetUserIdentifier

 protected virtual string GetUserIdentifier(HttpRequestMessage request)
 {
     return request.GetClientIpAddress();
 }
開發者ID:TGHGH,項目名稱:MesDemo,代碼行數:4,代碼來源:ThrottlingHandler.cs

示例15: GetUserIdentifier

 //override added to support OWIN, as at the time of writing, ThrottlingHandler did not support OWIN host
 protected override string GetUserIdentifier(HttpRequestMessage request)
 {
     var id = request.GetClientIpAddress();
     return id;
 }
開發者ID:diouf,項目名稱:apress-recipes-webapi,代碼行數:6,代碼來源:OwinCompatibleThrottlingHandler.cs


注:本文中的System.Net.Http.HttpRequestMessage.GetClientIpAddress方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。