其他分享
首页 > 其他分享> > Dubbo获取Request、Cookie

Dubbo获取Request、Cookie

作者:互联网

Dubbo获取 Request:

  HttpServletRequest request=  (HttpServletRequest) RpcContext.getContext().getRequest();

Dubbo获取 Cookie数组:

  HttpServletRequest request=  (HttpServletRequest) RpcContext.getContext().getRequest();
  Cookie[] cookies = request.getCookies();

Dubbo获取 Cookie中的值:

public class RpcContextUtil {

    /**
     * 获取cookie中的信息
     * @param key
     * @return
     */
    public static String getFromCookie(String key) {
        String value = "";
        if (StringUtils.isEmpty(key)) {
            return value;
        }
        //获取request
        HttpServletRequest request=  (HttpServletRequest) RpcContext.getContext().getRequest();
        //从request中获取Cookie数组
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            return value;
        }
        //从cookie数组中,获取key对应的value
        for (Cookie cookie: cookies) {
            if (key.equals(cookie.getName())) {
                value = cookie.getValue();
            }
        }
        //解码
        try {
            value = StringUtils.trim(URLDecoder.decode(value, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            log.error("cookie info could not decode.key: {}, value: {}",key, value);
        }
        return value;
    }

}

标签:HttpServletRequest,Dubbo,Request,request,value,Cookie,key,cookie
来源: https://www.cnblogs.com/expiator/p/16072536.html