• tomcat Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986


    1.情景展示

      tomcat 日志时不时会报出如下异常信息,到底是怎么回事?

    java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
    	at org.apache.coyote.http11.AbstractNioInputBuffer.parseRequestLine(AbstractNioInputBuffer.java:283)
    	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1017)
    	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
    	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    	at java.lang.Thread.run(Thread.java:745)

      页面无法打开

    2.原因分析  

      意思是:请求头中包含了 RFC 7230 and RFC 3986规范中定义的非法字符,在这种情况下就会导致页面报400异常。 

      原因就是:tomcat的版本过高造成的,网上说,tomcat高于 7.0.73的版本,添加了对于http头(请求头)的验证。

      get请求,即问号传参,就是只有请求头,没有请求体

      RFC3986文档规定,Url中只允许包含英文字母(a-zA-Z)、数字(0-9)、-_.~4个特殊字符以及所有保留字符。

      RFC3986中指定了以下字符为保留字符:! * ’ ( ) ; : @ & = + $ , / ? # [ ]

      同时RFC 3986规范在tomcat7.0.73版本中就已经提出了,RFC 7230也是对前者的一些补充或者说是完善,所以在tomcat7.0.73及以上版本都会有这种问题。 

      说明:这种情况,只在IE浏览器下会出现,因为IE浏览器不会对中文参数进行编码,而其它类型的浏览器会默认自动对中文进行编码。

    3.解决方案

      方法一:降低tomcat版本;

      经过测试发现,网上关于tomcat的最高版本要求描述有误,不是低于7.0.73就可以。

      我下载了一个tomcat7.0.70,运行项目后,还是会字符集的错误,导致网页无法打开。

      但是,我测了tomcat7.0.61,完美正常运行项目,控制台不再报错,网页可以正常打开了。

      64位下载地址:https://archive.apache.org/dist/tomcat/tomcat-7/v7.0.61/bin/apache-tomcat-7.0.61-windows-x64.zip

      32位下载地址:https://archive.apache.org/dist/tomcat/tomcat-7/v7.0.61/bin/apache-tomcat-7.0.61-windows-x86.zip

      方法二:将get请求改为post请求;(推荐使用)

      方法三:get请求(问号传参)

      使用URIEncoder()函数,将中文进行编码

    写在最后

      哪位大佬如若发现文章存在纰漏之处或需要补充更多内容,欢迎留言!!!

     相关推荐:

  • 相关阅读:
    Matlab图像处理函数:regionprops
    Java的 volatile关键字的底层实现原理
    SQL学习(一.索引)
    solr7.7.0搜索引擎使用(四)(搜索语法)
    solr7.7.0搜索引擎使用(三)(添加文件索引)
    singleton单例模式小结
    多态小结
    quartz定时任务时间设置
    EditPlus编译运行java文件
    NoClassDefFoundError错误发生的原因
  • 原文地址:https://www.cnblogs.com/Marydon20170307/p/10343098.html
Copyright © 2020-2023  润新知