HTTP status code

本文详细解析了常见的HTTP状态码,包括405 Method Not Allowed、404 Not Found、401 Unauthorized及400 Bad Request等,阐述了每种状态码的含义及其应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

被一个问题耽搁了好久,最后才恍然。这是关于HTTP status的。

使用feign进行http请求,结果总是抛出异常: read 405.由于不了解feign具体原理,还总觉得是内部错误。虽然错误信息没有明确指出http返回异常,但看到405就应该敏感才对。这里就记录遇到的各种status。

 

1.405 Method Not Allowed

请求方式不允许。即服务端只允许比如get,而你使用post获取则返回405.

The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource.

restful url的含义就是资源定位,所以请求的都是resource。通过get,post,delete,option等来确定对应的行为。当请求为request的时候,服务端会返回一个response。这个response的header会告诉你他允许的行为:

Allow →GET
Cache-Control →no-cache, no-store, max-age=0, must-revalidate
Content-Type →application/json;charset=UTF-8
Date →Wed, 03 Aug 2016 12:52:52 GMT
Expires →0
Pragma →no-cache
Strict-Transport-Security →max-age=31536000 ; includeSubDomains
Transfer-Encoding →chunked
X-Content-Type-Options →nosniff
X-Frame-Options →DENY
X-XSS-Protection →1; mode=block

比如服务端:

@RequestMapping(value = "/map.json", method = RequestMethod.GET)
    @ResponseBody
    public Map map(){
        Map map = new HashMap();
        map.put("name","Ryan");
        map.put("sex","man");
        map.put("age",18);
        List list = new ArrayList();
        list.add("red");
        list.add("black");
        list.add("blue");
        list.add("yellow");
        map.put("colors",list);
        return map;
    }
View Code

访问的request header为:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:zh-CN,zh;q=0.8
Authorization:Basic YWRtaW46dGVzdA==
Cache-Control:max-age=0
Connection:keep-alive
Host:localhost:8080
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36

访问的request主题general为:

Request URL:http://localhost:8080/hello/map.json
Request Method:GET
Status Code:200 
Remote Address:[::1]:8080

请求结果返回的response header为:

Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Type:application/json;charset=UTF-8
Date:Wed, 03 Aug 2016 13:08:38 GMT
Expires:0
Pragma:no-cache
Strict-Transport-Security:max-age=31536000 ; includeSubDomains
Transfer-Encoding:chunked
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block

显然没看到允许的行为是否是get,因为已经访问成功了。如果请求的行为不允许才会返回 Allow method.

 

2.404 Not Found

新闻乐见。url访问的路径在服务端找不到的时候返回404.即服务端的所有路由中都不匹配你所请求的url。

The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable.

 

3.401 Unauthorized (RFC 7235)

需要认证的接口,当header里authorization不匹配的时候就会返回401.

Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or has not yet been provided. The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource. See Basic access authentication and Digest access authentication.[36] 401 semantically means "unauthenticated",[37] i.e. the user does not have the necessary credentials.Note: Some sites issue HTTP 401 when an IP address is banned from the website (usually the website domain) and that specific address is refused permission to access a website.

 使用postman访问不带header里的authorization结果:

{
  "timestamp": 1470322895922,
  "status": 401,
  "error": "Unauthorized",
  "message": "Full authentication is required to access this resource",
  "path": "/hello/map.json"
}

 

 4.400 Bad request

The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.

 request不能被server识别,因为畸形(格式不符合要求)。如果request没有改变,则重复访问没有用,不应该重复访问。

from stackoverflow:

A 400 means that the request was malformed. In other words, the data stream sent by the client to the server didn't follow the rules.

In the case of a REST API with a JSON payload, 400's are typically, and correctly I would say, used to indicate that the JSON is invalid in some way according to the API specification for the service.

 request不符合要求。

 

 

4.

 

http://m.2cto.com/net/201605/511672.html

https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

https://zh.wikipedia.org/wiki/HTTP%E7%8A%B6%E6%80%81%E7%A0%81

https://en.wikipedia.org/wiki/HTTP_303

 

raise KeyError(f"{self.__class__.__name__} does not support field: {key}") KeyError: 'NepuContentItem does not support field: publish_date' 2025-07-07 20:46:23 [nepudata] INFO: 开始解析页面: https://www.nepu.edu.cn/gjjl/xjjl.htm 2025-07-07 20:46:23 [scrapy.core.scraper] ERROR: Spider error processing <GET https://www.nepu.edu.cn/gjjl/xjjl.htm> (referer: None) Traceback (most recent call last): File "D:\annaCONDA\Lib\site-packages\scrapy\utils\defer.py", line 257, in iter_errback yield next(it) ^^^^^^^^ File "D:\annaCONDA\Lib\site-packages\scrapy\utils\python.py", line 312, in __next__ return next(self.data) ^^^^^^^^^^^^^^^ File "D:\annaCONDA\Lib\site-packages\scrapy\utils\python.py", line 312, in __next__ return next(self.data) ^^^^^^^^^^^^^^^ File "D:\annaCONDA\Lib\site-packages\scrapy\core\spidermw.py", line 104, in process_sync for r in iterable: File "D:\annaCONDA\Lib\site-packages\scrapy\spidermiddlewares\offsite.py", line 28, in <genexpr> return (r for r in result or () if self._filter(r, spider)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\annaCONDA\Lib\site-packages\scrapy\core\spidermw.py", line 104, in process_sync for r in iterable: File "D:\annaCONDA\Lib\site-packages\scrapy\spidermiddlewares\referer.py", line 353, in <genexpr> return (self._set_referer(r, response) for r in result or ()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\annaCONDA\Lib\site-packages\scrapy\core\spidermw.py", line 104, in process_sync for r in iterable: File "D:\annaCONDA\Lib\site-packages\scrapy\spidermiddlewares\urllength.py", line 27, in <genexpr> return (r for r in result or () if self._filter(r, spider)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\annaCONDA\Lib\site-packages\scrapy\core\spidermw.py", line 104, in process_sync for r in iterable: File "D:\annaCONDA\Lib\site-packages\scrapy\spidermiddlewares\depth.py", line 31, in <genexpr> return (r for r in result or () if self._filter(r, response, spider)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\annaCONDA\Lib\site-packages\scrapy\core\spidermw.py", line 104, in process_sync for r in iterable: File "C:\Users\Lenovo\nepu_spider\nepu_spider\spiders\nepudata_spider.py", line 93, in parse item = self.extract_content(response) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Lenovo\nepu_spider\nepu_spider\spiders\nepudata_spider.py", line 175, in extract_content item['publish_date'] = publish_date ~~~~^^^^^^^^^^^^^^^^ File "D:\annaCONDA\Lib\site-packages\scrapy\item.py", line 85, in __setitem__ raise KeyError(f"{self.__class__.__name__} does not support field: {key}") KeyError: 'NepuContentItem does not support field: publish_date' 2025-07-07 20:46:23 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/yjsy/>: HTTP status code is not handled or not allowed 2025-07-07 20:46:24 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/jwc/>: HTTP status code is not handled or not allowed 2025-07-07 20:46:24 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/xsc/>: HTTP status code is not handled or not allowed 2025-07-07 20:46:25 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/rsc/>: HTTP status code is not handled or not allowed 2025-07-07 20:46:26 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/kyc/>: HTTP status code is not handled or not allowed 2025-07-07 20:46:26 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/xxyw/?year=2020>: HTTP status code is not handled or not allowed 2025-07-07 20:46:27 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/xxyw/?year=2021>: HTTP status code is not handled or not allowed 2025-07-07 20:46:28 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/xxyw/?year=2022>: HTTP status code is not handled or not allowed 2025-07-07 20:46:28 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/xxyw/?year=2023>: HTTP status code is not handled or not allowed 2025-07-07 20:46:29 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/xxyw/?year=2024>: HTTP status code is not handled or not allowed 2025-07-07 20:46:29 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/xxyw/?year=2025>: HTTP status code is not handled or not allowed 2025-07-07 20:46:30 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/tzgg/?year=2020>: HTTP status code is not handled or not allowed 2025-07-07 20:46:30 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/tzgg/?year=2021>: HTTP status code is not handled or not allowed 2025-07-07 20:46:31 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/tzgg/?year=2022>: HTTP status code is not handled or not allowed 2025-07-07 20:46:31 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/tzgg/?year=2023>: HTTP status code is not handled or not allowed 2025-07-07 20:46:32 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/tzgg/?year=2024>: HTTP status code is not handled or not allowed 2025-07-07 20:46:32 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/tzgg/?year=2025>: HTTP status code is not handled or not allowed 2025-07-07 20:46:33 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/xsdt/?year=2020>: HTTP status code is not handled or not allowed 2025-07-07 20:46:34 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/xsdt/?year=2021>: HTTP status code is not handled or not allowed 2025-07-07 20:46:34 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/xsdt/?year=2022>: HTTP status code is not handled or not allowed 2025-07-07 20:46:35 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/xsdt/?year=2023>: HTTP status code is not handled or not allowed 2025-07-07 20:46:36 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/xsdt/?year=2024>: HTTP status code is not handled or not allowed 2025-07-07 20:46:37 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <404 https://www.nepu.edu.cn/xsdt/?year=2025>: HTTP status code is not handled or not allowed 2025-07-07 20:46:37 [scrapy.core.engine] INFO: Closing spider (finished) 2025-07-07 20:46:37 [nepudata] INFO: 数据库连接已关闭 2025-07-07 20:46:37 [scrapy.statscollectors] INFO: Dumping Scrapy stats: {'downloader/request_bytes': 10178, 'downloader/request_count': 32, 'downloader/request_method_count/GET': 32, 'downloader/response_bytes': 117980, 'downloader/response_count': 32, 'downloader/response_status_count/200': 9, 'downloader/response_status_count/404': 23, 'elapsed_time_seconds': 23.999904, 'finish_reason': 'finished', 'finish_time': datetime.datetime(2025, 7, 7, 12, 46, 37, 62452), 'httpcompression/response_bytes': 187569, 'httpcompression/response_count': 9, 'httperror/response_ignored_count': 23, 'httperror/response_ignored_status_count/404': 23, 'log_count/ERROR': 9, 'log_count/INFO': 45, 'log_count/WARNING': 1, 'response_received_count': 32, 'scheduler/dequeued': 32, 'scheduler/dequeued/memory': 32, 'scheduler/enqueued': 32, 'scheduler/enqueued/memory': 32, 'spider_exceptions/KeyError': 9, 'start_time': datetime.datetime(2025, 7, 7, 12, 46, 13, 62548)} 2025-07-07 20:46:37 [scrapy.core.engine] INFO: Spider closed (finished)
最新发布
07-08
在 Scrapy 中出现 `KeyError: 'NepuContentItem does not support field: publish_date'` 错误的原因是尝试为一个未在 `Item` 类中定义的字段赋值。Scrapy 的 `Item` 类用于定义爬取数据的结构,所有需要存储的字段都必须在 `Item` 类中显式声明[^1]。 ### 解决方法 #### 1. 确保字段已在 `Item` 类中定义 首先检查你的 `Item` 类(例如 `NepuContentItem`)是否已经定义了 `publish_date` 字段: ```python import scrapy class NepuContentItem(scrapy.Item): title = scrapy.Field() content = scrapy.Field() publish_date = scrapy.Field() # 确保该字段已定义 ``` 如果尚未定义,请添加该字段并重新运行爬虫。 #### 2. 使用 `ItemLoader` 添加字段 如果你希望动态添加字段而不在 `Item` 类中显式声明,可以使用 `ItemLoader` 并设置 `loader.default_item_class` 或者使用 `dict` 来临时存储数据: ```python from scrapy.loader import ItemLoader def parse(self, response): item = ItemLoader(item=NepuContentItem()) item.add_value('title', '示例标题') item.add_value('publish_date', '2025-04-05') # 动态添加字段 yield item.load_item() ``` 或者直接使用字典来绕过 `Item` 的字段限制: ```python yield { 'title': '示例标题', 'publish_date': '2025-04-05' } ``` 注意:使用字典方式时,后续的 `Item Pipeline` 将无法自动处理这些数据,需手动处理。 #### 3. 检查字段名称拼写 确保你在爬虫中使用的字段名与 `Item` 类中定义的字段名完全一致,包括大小写和下划线使用: ```python # 正确 item['publish_date'] = '2025-04-05' # 错误(拼写错误) item['publishDate'] = '2025-04-05' ``` #### 4. 使用 `try-except` 处理异常字段 如果你不确定某些字段是否存在于 `Item` 中,可以使用 `try-except` 块进行容错处理: ```python try: item['publish_date'] = response.meta.get('date') except KeyError: self.logger.warning("字段 publish_date 未在 NepuContentItem 中定义") ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值