http响应总结:常见http响应错误总结

时间秒杀一切 提交于 2020-11-26 03:39:16

<div id="content_views" class="markdown_views"> <!-- flowchart 箭头图标 勿删 --> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"> <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path> </svg> <p>工作中经常会被同事问这个http请求为什么调不通,我虽然能解释清楚错误是什么,但是没有总结过,想到刚开始时,也是看了别人的文章才会的,所以总结一下,贡献一下自己的经验。</p>

<h2 id="http-404-响应">http 404 响应</h2>

<p>404响应可以说是,在java web中最常见的错误了,基本上大家都理解问题产生的原因,即找不到这个资源。</p>

<ul> <li><strong>路径匹配错误导致的404</strong></li> </ul>

<p>比如说请求的访问路径为<a href="http://localhost:8080/project/test" rel="nofollow" target="_blank">http://localhost:8080/project/test</a>,但是访问路径确实<a href="http://localhost:8080/project/tset" rel="nofollow" target="_blank">http://localhost:8080/project/tset</a>, 这样就会报404错误,这是最常见的; <br> 如果,项目名写错了呢? <br> 测试结果如下:</p>

<pre class="prettyprint"><code class=" hljs oxygene" onclick="mdcp.signin(event)" style="position: unset;">Request URL:http:<span class="hljs-comment">//localhost:8080/projects/test</span> Request <span class="hljs-function"><span class="hljs-keyword">Method</span>:</span>OPTIONS Status Code:<span class="hljs-number">404</span> <span class="hljs-keyword">Not</span> Found Remote Address:[::<span class="hljs-number">1</span>]:<span class="hljs-number">8080</span> Referrer Policy:no-referrer-when-downgrade<div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<p>仍然是404错误。</p>

<ul> <li><strong>如果路径端口或ip写错误</strong> <br> 如果把端口写错了,测试结果如下:</li> </ul>

<pre class="prettyprint"><code class=" hljs ruby" onclick="mdcp.signin(event)" style="position: unset;"><span class="hljs-constant">Request</span> <span class="hljs-constant">URL</span><span class="hljs-symbol">:http</span><span class="hljs-symbol">://localhost</span><span class="hljs-symbol">:</span><span class="hljs-number">8090</span>/project/test <span class="hljs-constant">Referrer</span> <span class="hljs-constant">Policy</span><span class="hljs-symbol">:no-referrer-when-downgrade</span> <span class="hljs-constant">Request</span> <span class="hljs-constant">Headers</span> <span class="hljs-constant">Provisional</span> headers are shown <span class="hljs-constant">Access</span>-<span class="hljs-constant">Control</span>-<span class="hljs-constant">Request</span>-<span class="hljs-constant">Headers</span><span class="hljs-symbol">:content-type</span> <span class="hljs-constant">Access</span>-<span class="hljs-constant">Control</span>-<span class="hljs-constant">Request</span>-<span class="hljs-constant">Method</span><span class="hljs-symbol">:POST</span> <span class="hljs-constant">Origin</span><span class="hljs-symbol">:http</span><span class="hljs-symbol">://localhost</span><span class="hljs-symbol">:</span><span class="hljs-number">8010</span> <span class="hljs-constant">Referer</span><span class="hljs-symbol">:http</span><span class="hljs-symbol">://localhost</span><span class="hljs-symbol">:</span><span class="hljs-number">8010</span>/ <span class="hljs-constant">User</span>-<span class="hljs-constant">Agent</span><span class="hljs-symbol">:Mozilla/</span><span class="hljs-number">5.0</span> (<span class="hljs-constant">Windows</span> <span class="hljs-constant">NT</span> <span class="hljs-number">6.1</span>; <span class="hljs-constant">WOW64</span>) <span class="hljs-constant">AppleWebKit</span>/<span class="hljs-number">537.36</span> (<span class="hljs-constant">KHTML</span>, like <span class="hljs-constant">Gecko</span>) <span class="hljs-constant">Chrome</span>/<span class="hljs-number">59.0</span>.<span class="hljs-number">3071.115</span> <span class="hljs-constant">Safari</span>/<span class="hljs-number">537.36</span><div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<p>没有状态值,也没有错误信息,所以看到这样的请求,可以查看一下是否端口写错了。</p>

<p>如果把ip写错了,测试结果如下:</p>

<pre class="prettyprint"><code class=" hljs ruby" onclick="mdcp.signin(event)" style="position: unset;"><span class="hljs-constant">Request</span> <span class="hljs-constant">URL</span><span class="hljs-symbol">:http</span><span class="hljs-symbol">://www</span>.baidu.<span class="hljs-symbol">com:</span><span class="hljs-number">8090</span>/project/test <span class="hljs-constant">Referrer</span> <span class="hljs-constant">Policy</span><span class="hljs-symbol">:no-referrer-when-downgrade</span> <span class="hljs-constant">Request</span> <span class="hljs-constant">Headers</span> <span class="hljs-constant">Provisional</span> headers are shown <span class="hljs-constant">Access</span>-<span class="hljs-constant">Control</span>-<span class="hljs-constant">Request</span>-<span class="hljs-constant">Headers</span><span class="hljs-symbol">:content-type</span> <span class="hljs-constant">Access</span>-<span class="hljs-constant">Control</span>-<span class="hljs-constant">Request</span>-<span class="hljs-constant">Method</span><span class="hljs-symbol">:POST</span> <span class="hljs-constant">Origin</span><span class="hljs-symbol">:http</span><span class="hljs-symbol">://localhost</span><span class="hljs-symbol">:</span><span class="hljs-number">8010</span> <span class="hljs-constant">Referer</span><span class="hljs-symbol">:http</span><span class="hljs-symbol">://localhost</span><span class="hljs-symbol">:</span><span class="hljs-number">8010</span>/ <span class="hljs-constant">User</span>-<span class="hljs-constant">Agent</span><span class="hljs-symbol">:Mozilla/</span><span class="hljs-number">5.0</span> (<span class="hljs-constant">Windows</span> <span class="hljs-constant">NT</span> <span class="hljs-number">6.1</span>; <span class="hljs-constant">WOW64</span>) <span class="hljs-constant">AppleWebKit</span>/<span class="hljs-number">537.36</span> (<span class="hljs-constant">KHTML</span>, like <span class="hljs-constant">Gecko</span>) <span class="hljs-constant">Chrome</span>/<span class="hljs-number">59.0</span>.<span class="hljs-number">3071.115</span> <span class="hljs-constant">Safari</span>/<span class="hljs-number">537.36</span><div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<p>还是这样的错误,所以遇到这样的错误,不仅要查看端口,还要查看ip是不是对的。 <br> - <strong>总结</strong> <br> 路径写错了,一般会导致404错误,还有可能导致无法调通,遇到以上错误,可查找对应的原因,解决错误。遇到404了,不要总想着自己没错。总有同事会说,怎么可能写错,我复制过来的,可能你确实是复制的接口文档的内容,但是后台开发的同事改了或者写错了,与其纠结到底谁的责任,不如早点查到问题原因。</p>

<h2 id="http-400响应">http 400响应</h2>

<p>http 400错误,也算是一个比较常见的错误,这种错误的原因非常明确,就是请求的数据和接受的数据无法匹配,有一下两种常见情况:</p>

<ul> <li><strong>数据格式错误</strong> <br> 我初期经常遇到这种错误,比如说,我的后台接口接受参数的代码如下:</li> </ul>

<pre class="prettyprint"><code class=" hljs cmake" onclick="mdcp.signin(event)" style="position: unset;">public <span class="hljs-keyword">Message</span> getTestInfo(@RequestBoday Test test) { <span class="hljs-keyword">Message</span> <span class="hljs-keyword">message</span> = new <span class="hljs-keyword">Message</span>(); `````省略 <span class="hljs-keyword">return</span> <span class="hljs-keyword">message</span> }<div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<p>基本上,用过的一看就明白,这是接受一个json字符串格式的数据,把数据映射到Test类上。而那时候,我用jquery ajax提交数据经常这么写:</p>

<pre class="prettyprint"><code class=" hljs javascript" onclick="mdcp.signin(event)" style="position: unset;">$.ajax({ type: <span class="hljs-string">"POST"</span>, url: <span class="hljs-string">"test"</span>, contentType: <span class="hljs-string">"application/json; charset=utf-8"</span>, data: form.serialize(), dataType: <span class="hljs-string">"json"</span>, success: <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(message)</span> {</span> <span class="hljs-keyword">if</span> (message &gt; <span class="hljs-number">0</span>) { alert(<span class="hljs-string">"请求已提交!我们会尽快与您取得联系"</span>); } }, error: <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(message)</span> {</span> $(<span class="hljs-string">"#request-process-patent"</span>).html(<span class="hljs-string">"提交数据失败!"</span>); } });<div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<p>当时还不懂这东西,都是跟着别人写,记得当时同事都下班,自己写的时候遇到这种错误,想的头都疼了都不知道怎么改。 <br> 返回的错误信息如下:</p>

<pre class="prettyprint"><code class=" hljs xml" onclick="mdcp.signin(event)" style="position: unset;"><span class="hljs-tag">&lt;<span class="hljs-title">html</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-title">head</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"Content-Type"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"text/html;charset=utf-8"</span>/&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-title">title</span>&gt;</span>Error 400 Bad Request<span class="hljs-tag">&lt;/<span class="hljs-title">title</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-title">head</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-title">body</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">h2</span>&gt;</span>HTTP ERROR 400<span class="hljs-tag">&lt;/<span class="hljs-title">h2</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-title">p</span>&gt;</span>Problem accessing /project/test. Reason: <span class="hljs-tag">&lt;<span class="hljs-title">pre</span>&gt;</span> Bad Request<span class="hljs-tag">&lt;/<span class="hljs-title">pre</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">p</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">hr</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"http://eclipse.org/jetty"</span>&gt;</span>Powered by Jetty:// 9.4.1.v20170120<span class="hljs-tag">&lt;/<span class="hljs-title">a</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">hr</span>/&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-title">body</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-title">html</span>&gt;</span><div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<p>错误很明显,数据实际格式表单格式,后台接受json字符串格式,怎么可能对?(这里有一个前提条件,前后的请求类型要一致,后台只接收post请求,前台发的也是post请求,如果发的是get请求,就会报Error 405 Request method 'GET' not supported 错误)。</p>

<ul> <li><strong>字段数据类型不一致</strong></li> </ul>

<p>这种问题很常见,比如给数值字段传了一个含有非数值的字符串,给数组或者集合传了字符串; <br> 下面是接收数据的对象,接收的数据格式为json字符串:</p>

<pre class="prettyprint"><code class=" hljs cs" onclick="mdcp.signin(event)" style="position: unset;"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Test{ <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> test; }<div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<p>我发送的参数的格式</p>

<pre class="prettyprint"><code class=" hljs json" onclick="mdcp.signin(event)" style="position: unset;">{ "<span class="hljs-attribute">test</span>":<span class="hljs-value"><span class="hljs-string">"abc"</span> </span>}<div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<p>测试结果:</p>

<pre class="prettyprint"><code class=" hljs xml" onclick="mdcp.signin(event)" style="position: unset;"><span class="hljs-tag">&lt;<span class="hljs-title">html</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-title">head</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"Content-Type"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"text/html;charset=utf-8"</span>/&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-title">title</span>&gt;</span>Error 400 Bad Request<span class="hljs-tag">&lt;/<span class="hljs-title">title</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-title">head</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-title">body</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">h2</span>&gt;</span>HTTP ERROR 400<span class="hljs-tag">&lt;/<span class="hljs-title">h2</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-title">p</span>&gt;</span>Problem accessing /cybx-core-service/role/test. Reason: <span class="hljs-tag">&lt;<span class="hljs-title">pre</span>&gt;</span> Bad Request<span class="hljs-tag">&lt;/<span class="hljs-title">pre</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">p</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">hr</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"http://eclipse.org/jetty"</span>&gt;</span>Powered by Jetty:// 9.4.1.v20170120<span class="hljs-tag">&lt;/<span class="hljs-title">a</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">hr</span>/&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-title">body</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-title">html</span>&gt;</span><div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<p>出现400错误了。</p>

<ul> <li><strong>总结</strong></li> </ul>

<p>所以遇见400错误,只要从这两方面找问题,基本都能解决问题。</p>

<h2 id="http-415响应">http 415响应</h2>

<p>遇见415响应的问题相对较少一些,而且这个响应的问题也非常明确,前后台媒体类型不一致,可以简单理解为content-type不一致。</p>

<pre class="prettyprint"><code class=" hljs cs" onclick="mdcp.signin(event)" style="position: unset;"> @RequestMapping(<span class="hljs-keyword">value</span> = <span class="hljs-string">"test"</span>, method = RequestMethod.POST) <span class="hljs-keyword">public</span> Message <span class="hljs-title">test</span>(@RequestBody Test test) { Message message = <span class="hljs-keyword">new</span> Message(); System.<span class="hljs-keyword">out</span>.println(<span class="hljs-string">"参数是=="</span> + test.getRname()); <span class="hljs-keyword">return</span> message; }<div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<p>上面这种写法,是在spring mvc框架的项目中常见的写法,接收post请求,接收json字符串参数,下面是请求:</p>

<pre class="prettyprint"><code class=" hljs lasso" onclick="mdcp.signin(event)" style="position: unset;"> Request URL:http:<span class="hljs-comment">//localhost:8010/project/role/test</span> Request Method:POST Status Code:<span class="hljs-number">415</span> Unsupported Media <span class="hljs-keyword">Type</span> Remote Address:<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">8010</span> <span class="hljs-keyword">Referrer</span> Policy:no<span class="hljs-attribute">-referrer</span><span class="hljs-attribute">-when</span><span class="hljs-attribute">-downgrade</span> Accept:application/json, text/plain, <span class="hljs-subst">*</span><span class="hljs-comment">/* Accept-Encoding:gzip, deflate, br Accept-Language:zh-CN,zh;q=0.8 Connection:keep-alive Content-Length:29 Content-Type:application/x-www-form-urlencoded;charset=UTF-8 {"test":"123"}:</span><div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<p>这是返回的响应:</p>

<pre class="prettyprint"><code class=" hljs xml" onclick="mdcp.signin(event)" style="position: unset;"><span class="hljs-tag">&lt;<span class="hljs-title">html</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-title">head</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"Content-Type"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"text/html;charset=utf-8"</span>/&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-title">title</span>&gt;</span>Error 415 Unsupported Media Type<span class="hljs-tag">&lt;/<span class="hljs-title">title</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-title">head</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-title">body</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">h2</span>&gt;</span>HTTP ERROR 415<span class="hljs-tag">&lt;/<span class="hljs-title">h2</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-title">p</span>&gt;</span>Problem accessing /cybx-core-service/role/test. Reason: <span class="hljs-tag">&lt;<span class="hljs-title">pre</span>&gt;</span> Unsupported Media Type<span class="hljs-tag">&lt;/<span class="hljs-title">pre</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">p</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">hr</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"http://eclipse.org/jetty"</span>&gt;</span>Powered by Jetty:// 9.4.1.v20170120<span class="hljs-tag">&lt;/<span class="hljs-title">a</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">hr</span>/&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-title">body</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-title">html</span>&gt;</span><div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<p>由此可知,即使实体中的数据是json字符串格式,请求也是post请求,只要content-type不一致,就会报415错误。</p>

<ul> <li><strong>总结</strong> <br> 遇到415错误,首先要确定接口到底要接收什么格式的数据,然后再看一下请求发送的是什么格式,问题基本上就解决了。 <br> 但是当你不知道自己调用的接口是需要什么格式的数据的时候,请先确定接口类型;如果是调用第三方接口,就向第三方接口索要接口对接文档,如果是同事写的,就向同事确认接口类型;我的同事对接第三方接口时,经常遇到不明不白的接口,做的第一件事就是打电话和qq向他们要资料。</li> </ul>

<h2 id="http-320响应">http 320响应</h2>

<p>302响应遇到相当少的一个错误,大概只遇到不到10次,但是有点诡异的一个错误,只要去网上搜,基本都会看到临时重定向这个解释。我的同事告诉我这是网络的问题,和代码没有关系,只能怪运气不好,刷新一下就好了。 <br> 但是,我遇到了几次,全都是因为后台代码执行出现了错误导致的,有人会说代码出错,不应该报500响应么,怎么会是302。 <br> 这就是我说的诡异的情况,代码执行出错却报了302错误。可惜的是我没有保留当时的日志文件,我现在无法重现这个错误。</p>

<p>所以,我建议,如果遇到302错误,先不要把错误推给重定向,也有可能是代码出错了,找到详细的执行日志,看看有没有错误,有可能就是错误导致的。</p>

<h2 id="http-500响应">http 500响应</h2>

<p>500响应,这是最常见额错误,一般都是后台代码执行出错了,只要找到错误代码修改就行,没什么好说的。</p>

<h2 id="总结">总结</h2>

<p>以上就是我处理http响应的经验,希望能帮到他人,也算一个总结。</p> 原文地址:https://blog.csdn.net/u010173095/article/details/77652107 </div>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!