当前位置: 首页>>代码示例>>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;未经允许,请勿转载。