天天看点

httpclient正确请求可以接受到数据,当发送错误参数时返回null

一、问题描述 

在postman接口调试中响应的数据都是正常的,使用httpclient当传送错误参数时响应为null,传送正常数据时却可以接收到数据

二、解决问题

问题的原因也是因为懒惰,每次使用工具类时直接在网上摘抄,没有好好关注过工具类的具体实现。下边是在网上随便搜索的httpclient工具类。

httpclient正确请求可以接受到数据,当发送错误参数时返回null

以下是httpstatus类的状态详解。

  1. //Informational 1xx 信息
  2. '100' : 'Continue', //继续
  3. '101' : 'Switching Protocols', //交换协议
  4. //Successful 2xx 成功
  5. '200' : 'OK', //OK
  6. '201' : 'Created', //创建
  7. '202' : 'Accepted', //已接受
  8. '203' : 'Non-Authoritative Information', //非权威信息
  9. '204' : 'No Content', //没有内容
  10. '205' : 'Reset Content', //重置内容
  11. '206' : 'Partial Content', //部分内容
  12. //Redirection 3xx 重定向
  13. '300' : 'Multiple Choices', //多种选择
  14. '301' : 'Moved Permanently', //永久移动
  15. '302' : 'Found', //找到
  16. '303' : 'See Other', //参见其他
  17. '304' : 'Not Modified', //未修改
  18. '305' : 'Use Proxy', //使用代理
  19. '306' : 'Unused', //未使用
  20. '307' : 'Temporary Redirect', //暂时重定向
  21. //Client Error 4xx 客户端错误
  22. '400' : 'Bad Request', //错误的请求
  23. '401' : 'Unauthorized', //未经授权
  24. '402' : 'Payment Required', //付费请求
  25. '403' : 'Forbidden', //禁止
  26. '404' : 'Not Found', //没有找到
  27. '405' : 'Method Not Allowed', //方法不允许
  28. '406' : 'Not Acceptable', //不可接受
  29. '407' : 'Proxy Authentication Required', //需要代理身份验证
  30. '408' : 'Request Timeout', //请求超时
  31. '409' : 'Conflict', //指令冲突
  32. '410' : 'Gone', //文档永久地离开了指定的位置
  33. '411' : 'Length Required', //需要Content-Length头请求
  34. '412' : 'Precondition Failed', //前提条件失败
  35. '413' : 'Request Entity Too Large', //请求实体太大
  36. '414' : 'Request-URI Too Long', //请求URI太长
  37. '415' : 'Unsupported Media Type', //不支持的媒体类型
  38. '416' : 'Requested Range Not Satisfiable', //请求的范围不可满足
  39. '417' : 'Expectation Failed', //期望失败
  40. //Server Error 5xx 服务器错误
  41. '500' : 'Internal Server Error', //内部服务器错误
  42. '501' : 'Not Implemented', //未实现
  43. '502' : 'Bad Gateway', //错误的网关
  44. '503' : 'Service Unavailable', //服务不可用
  45. '504' : 'Gateway Timeout', //网关超时
  46. '505' : 'HTTP Version Not Supported' //HTTP版本不支持

网上所有的client工具类几乎都是默认服务器返回200的状态时才会解析收到的数据。而有的接口会根据规定修改响应的返回状态。因此调试接口时一定要注意这点