请教个问题,restful接口同时提供给PC、移动端去调用,你们是如何区分出来的

请教个问题,restful接口同时提供给PC、移动端去调用,你们是如何区分出来的,
如PC传的是json串、内容不加密、移动端需要加密

6个回答

百度关键字:利用请求头判断是手机端还是PC端

根据请求头的User-Agent

在拟成appkey的时候就应该区分嘛,MD5加密,一个32位,一个16位,就能区分了。

不能改的话看看这个能不能

  @RequestMapping(value="/test")
    @ResponseBody
    public void test1(HttpServletRequest request,Questionnaire quest,String  questOptions){
        String ipAddress = null;
         if (request.getHeader("x-forwarded-for") == null) {  
             ipAddress = request.getRemoteAddr();
         }else{
            if(request.getHeader("x-forwarded-for").length()  > 15){
                String [] aStr = request.getHeader("x-forwarded-for").split(",");
                ipAddress = aStr[0];
            } else{
                ipAddress = request.getHeader("x-forwarded-for");
            }
         }  

         String terminal = request.getHeader("User-Agent");
        if(terminal.contains("Windows NT")){
            terminal = "PC端";
        }else{
            terminal = "移动端";
        }
        System.out.println("访客IP:"+ipAddress);
        System.out.println("用户访问来源:"+terminal);
        System.out.println("访问URL:"+request.getRequestURL());
    }
longlong3207
longlong3207 这个是访问servlet的时候获取
接近 3 年之前 回复

User-Agent通常就可以了

根据请求头的User-Agent

 1、第一种:

function IsPC() {
    var userAgentInfo = navigator.userAgent;
    var Agents = ["Android", "iPhone",
                "SymbianOS", "Windows Phone",
                "iPad", "iPod"];
    var flag = true;
    for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) {
            flag = false;
            break;
        }
    }
    return flag;
}

2、第二种:

function browserRedirect() {
    var sUserAgent = navigator.userAgent.toLowerCase();
    var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
    var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
    var bIsMidp = sUserAgent.match(/midp/i) == "midp";
    var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
    var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
    var bIsAndroid = sUserAgent.match(/android/i) == "android";
    var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
    var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
    if (!(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) ){
        window.location.href=B页面;
    }
}
browserRedirect();
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问