System.Web.NullPointerException

在.Net异步webApi中我们需要记录日志信息,需要获取客户端的ip地址,我们需要使用:HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];来获取客户端的ip地址,在调用异步方法(wait Task.Run(() =>{  }))前需要将主线程中获取的HttpContext.Current对象存至缓存(Cache)中达到多线程共享的目的。如果不是通过主线程获取HttpContext.Current对象将会报空指针异常(NullPointerException)。

示例代码:

1 System.Web.HttpRuntime.Cache.Insert("context", System.Web.HttpContext.Current); //异步调用,HttpContext存入缓存线程共享
2 wait Task.Run(() =>{  });

工具类方法示例代码:

 1         /// <summary>
 2         /// 获取客户端IP地址(无视代理)
 3         /// </summary>
 4         /// <returns>若失败则返回回送地址</returns>
 5         public static string GetHostAddress()
 6         {
 7 
 8             HttpContext httpContext = HttpContext.Current;
 9             if (httpContext == null)
10                 httpContext = HttpRuntime.Cache.Get("context") as HttpContext;
11             string userHostAddress = httpContext.Request.ServerVariables["REMOTE_ADDR"];
12 
13             if (string.IsNullOrEmpty(userHostAddress))
14             {
15                 if (httpContext.Request.ServerVariables["HTTP_VIA"] != null)
16                     userHostAddress = httpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(',')[0].Trim();
17             }
18             if (string.IsNullOrEmpty(userHostAddress))
19             {
20                 userHostAddress = httpContext.Request.UserHostAddress;
21             }
22 
23             //最后判断获取是否成功,并检查IP地址的格式(检查其格式非常重要)
24             if (!string.IsNullOrEmpty(userHostAddress) && IsIP(userHostAddress))
25             {
26                 return userHostAddress;
27             }
28             return "127.0.0.1";
29         }

 

转载于:https://www.cnblogs.com/netlws/p/11059207.html

java.lang.NullPointerException: null at com.datech.web.controller.system.TbappController.remove(TbappController.java:667) at com.datech.web.controller.system.TbappController$$FastClassBySpringCGLIB$$ea1c3ba.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:55) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) at org.apache.shiro.spring.security.interceptor.AopAllianceAnnotationsAuthorizingMethodInterceptor$1.proceed(AopAllianceAnnotationsAuthorizingMethodInterceptor.java:82) at org.apache.shiro.authz.aop.AuthorizingMethodInterceptor.invoke(AuthorizingMethodInterceptor.java:39) at org.apache.shiro.spring.security.interceptor.AopAllianceAnnotationsAuthorizingMethodInterceptor.invoke(AopAllianceAnnotationsAuthorizingMethodInterceptor.java:115) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691) at com.datech.web.controller.system.TbappController$$EnhancerBySpringCGLIB$$b3b02033.remove(<generated>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值