简体   繁体   English

如何更改 HttpServletRequest 的用户代理?

[英]how to change the user agent of HttpServletRequest?

my application is using HttpServletRequest to send a webservice request.我的应用程序正在使用 HttpServletRequest 发送网络服务请求。 when i run the request from our application in ie11 i get no results returned, but when i run it from Firefox i get results back.当我在 ie11 中从我们的应用程序运行请求时,我没有返回任何结果,但是当我从 Firefox 运行它时,我得到了结果。 this is what aRequest.getHeader("User-Agent") current value这是 aRequest.getHeader("User-Agent") 当前值

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.2; .NET CLR 1.1.4322; MS-RTC LM 8) Mozilla/4.0(兼容;MSIE 7.0;Windows NT 6.1;WOW64;Trident/7.0;SLCC2;.NET CLR 2.0.50727;.NET CLR 3.5.30729;.NET CLR 3.0.30729;Media Center PC 6.0; 0C;.NET4.0E;InfoPath.2;.NET CLR 1.1.4322;MS-RTC LM 8)

i need to change this to ie11 compatibilty.我需要将其更改为 ie11 兼容性。 it looks like the user agent in the request is for ie7.0 the problem is i dont see any options for 'setHeader'.看起来请求中的用户代理是针对 ie7.0 的,问题是我没有看到“setHeader”的任何选项。 i only see option for aRequest.setAttribute i have tried setting attribute but that did not work我只看到 aRequest.setAttribute 的选项我试过设置属性但没有用

 public final ActionForward execute(final ActionMapping aMapping,
                             final ClientForm aForm,
                             final HttpServletRequest aRequest,
                             final HttpServletResponse aResponse)
                  throws ServletException
{
    try {

        final ServletOutputStream out = aResponse.getOutputStream(); 
        aResponse.setContentType("text/html");
        processAjaxRequest(aMapping, aForm, aRequest, aResponse, out);
         out.flush();
        out.close();
    }

 public void processAjaxRequest(final ActionMapping aMapping,
                                 final ClientForm aForm,
                                 final HttpServletRequest aRequest,
                                 final HttpServletResponse aResponse,
                                 final ServletOutputStream out)
                      throws ServletException, IOException
    {

            String response = null;
            boolean isValid = false;

            if (isValid(aRequest, aForm)) { 
                response = "success";
                isValid = true;
            } else {
                response = "fail";
                isValid = false;
            }
            if (WebUtils.getParameter(aRequest, "format", "html").equals("json")) {
                //Map<String,String> map = new HashMap<String,String>(1);
                Map map = new HashMap(1);
                map.put("status", isValid ? "ok" : "fail");
                aResponse.setHeader("X-JSON", JSON.encode(map));
            } else {
                out.print(response);
            }        
    }

my application is using HttpServletRequest to send a webservice request.我的应用程序正在使用 HttpServletRequest 发送网络服务请求。

Your application is webservice.您的应用程序是网络服务。 Webservice doesn't send request, it receive requests from the client. Webservice 不发送请求,它接收来自客户端的请求。

HttpServletRequest is request from your client (browser in your case). HttpServletRequest 是来自您的客户端(在您的情况下为浏览器)的请求。 You shouldn't change it, for you servlet it's just "read-only" input data to handle.您不应该更改它,对于您的 servlet,它只是要处理的“只读”输入数据。

Browser send such user-agent for some historical reason ( more details here ).浏览器出于某种历史原因发送这样的用户代理(更多细节在这里)。 But "compatible; MSIE 7.0;"但是“兼容;MSIE 7.0;” in user-agent request has nothing common with your problem with ie11.在用户代理请求中与您的 ie11 问题没有任何共同之处。

You should investigate deeper reason, why ie11 doesn't work.您应该调查更深层次的原因,为什么 ie11 不起作用。 Look at console in browser (any errors?) or debugging your service code.在浏览器中查看控制台(有任何错误吗?)或调试您的服务代码。

I see, that you write response "out.print(response);"我明白了,你写了 response "out.print(response);" only in "else" case of your condition, may be it's a reason.仅在您的情况的“其他”情况下,可能是一个原因。 So, if condition is true, your code doesn't send body (only "X-JSON" header).因此,如果条件为真,您的代码不会发送正文(仅“X-JSON”标头)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM