spring mvc處理ios 請求頭不全時空參 沒法解析的問題處理

ios前端比較懶~囧~~,還有已安裝的app用戶 ,不肯意更新等問題 致使 請求頭沒法補全給服務端springmvc發送過來。。。。。悲劇了html

只能服務端本身處理這個不全的http請求空參沒法解析的問題前端

 嘗試處理方法java

1     增長springmvc過濾器對請求進行過濾(失敗)ios

測試發如今訪問控制層匹配請求地址以前就已經報錯,應該是dispatcher那裏出了問題web

雖然失敗, 把代碼配置貼出來~~spring

applicationContext.xml數據庫

<mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.interceptor.ForIosParam"></bean>
        </mvc:interceptor>
    </mvc:interceptors>apache

ForIosParam.javajson

public class ForIosParam implements HandlerInterceptor {瀏覽器

@Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        StringBuffer url=request.getRequestURL();
        Map requestParams=request.getParameterMap();
        Method method=requestParams.getClass().getMethod("setLocked",new Class[]{boolean.class});
        method.invoke(requestParams,new Object[]{new Boolean(false)});
        requestParams.put("ForIosParam", "ForIosParam");
        Map map1=request.getParameterMap();
        request.getRequestURL().append("?ForIosParam=ForIosParam");
        request.setAttribute("ForIosParam", "ForIosParam");
        StringBuffer url1=request.getRequestURL();
        return true;
    }

}


2  使用servlet在接收到請求以後發送再次本身給本身發送一個http完整的請求 (失敗,爲毛請求發出去了,而後就沒有而後了一點反應也木有。。。。必須異步的嗎? 無論那麼多了。。。換)這個網上搜就能夠java發送http  post請求

3  使用 servlet利用post在接收到請求以後,增長參數再服務器跳轉forward到springmvc 的get方式接收的方法裏(失敗啊。。。。。每次都是servlet攔截到。。。。。悲催  爲毛   無論那麼多 。。。。換)

代碼以下

package com.util;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.model.umaiw_app_system;
import com.service.GetInitKeyService;
import com.service.GetTheBestServerService;
import com.service.implement.GetInitKeyServiceImpl;
import com.service.implement.GetTheBestServerServiceImpl;
import com.test.HttpRequest;

public class getTheBestServerServlet extends HttpServlet{

    public void doPost (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/json; charset=gb2312");  
         Map requestParams=request.getParameterMap();
            Method method;
            try {
                method = requestParams.getClass().getMethod("setLocked",new Class[]{boolean.class});
                method.invoke(requestParams,new Object[]{new Boolean(false)});
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            requestParams.put("ForIosParam", "ForIosParam");
            Map map1=request.getParameterMap();
            request.getRequestURL().append("?ForIosParam=ForIosParam");
            request.setAttribute("ForIosParam", "ForIosParam");
        ServletContext sc = getServletContext();    
        RequestDispatcher rd = null;    
        rd = sc.getRequestDispatcher(request.getRequestURI()); //定向的頁面    
        rd.forward(request, response);    
        StringBuffer url=request.getRequestURL();
//        response.sendRedirect(url.toString());
    }
    
}

5 乾脆 直接用servlet吧無參的http請求處理掉(成功啦~~ )

不過這種方式由於是單獨走的servlet沒有走spring mvc和spring框架的支持(木有jdbcTemplate,木有自動注入,木有數據庫鏈接池~~~OMG  對付吧 ,無論三七二十一拼了,誰讓他不補全標準的http請求噠)

代碼以下(不能注入就實例化)

package com.util;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.model.umaiw_app_system;
import com.service.GetInitKeyService;
import com.service.GetTheBestServerService;
import com.service.implement.GetInitKeyServiceImpl;
import com.service.implement.GetTheBestServerServiceImpl;
import com.test.HttpRequest;

public class getTheBestServerServlet extends HttpServlet{

    public void doPost (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        GetInitKeyServiceImpl getInitKeyService=new GetInitKeyServiceImpl();
        GetTheBestServerServiceImpl getTheBestServerService= new GetTheBestServerServiceImpl();
        Map<String, String> map = new HashMap<String, String>();
        map.put("request", "getTheBestServer");
        map.put("status", "0");

//阿里雲獲取真實ip,這個也讓人頭疼。。。。。
        String ip=request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        String initKey ="";
        umaiw_app_system uas = getInitKeyService.getinitKey(ip);
        if ( null== uas ) {
            String uuid = UUID.randomUUID().toString();
            initKey = uuid.substring(uuid.length()-8);
            
            String uuidkey = UUID.randomUUID().toString();
            String key = uuidkey.substring(uuidkey.length()-8);
            try {
                getTheBestServerService.insertKey(ip, initKey, new Date(),key);
            } catch (Exception e) {
                map.put("status", "1");
            }
        } else {
            initKey=uas.getInit_key();
        }
        

        try {
            map.put("serverIp", DesUtil.encrypt(
            "XX.XX.XX.XX"(可使負載均衡的ip),initKey));
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("initKey", initKey);
        map.put("ip", ip);
        
        JSONObject jsonObject = JSONObject.fromObject(map);
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write(jsonObject.toString());
    }
}

web.xml

<servlet>
    <servlet-name>getTheBestServerServlet</servlet-name>
    <servlet-class>com.util.getTheBestServerServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>getTheBestServerServlet</servlet-name>
    <url-pattern>/system/getTheBestServer</url-pattern>
  </servlet-mapping>

數據庫訪問層用最基本的jdbc訪問(別忘了使用完鏈接 關了它,,,,,)

附上 阿里雲獲取真實ip的tomcat下

server.xml最後一行的配置

<Valve className="org.apache.catalina.valves.AccessLogValve" pattern="%{X-Forwarded-For}i %h %l %u %t "%r" %s %b" suffix=".txt" prefix="localhost_access_log." directory="logs"/>

大功告成 這樣就能夠單獨處理無參不全請求頭的死ios的http請求啦。。。。。

若是是json字符串請求服務器端的話 springmvc默認 是text/plian這種鍵值對的接收方式 (也就是瀏覽器發送過來的形式)

可是 若是是 application/json這種的contenttype的話 是接收不到參數的 ,(在攔截器裏設置請求的請求的contenttype也不行)只好讓手機端改爲鍵值對的形式

相關文章
相關標籤/搜索