本文共 785 字,大约阅读时间需要 2 分钟。
最近在写一个Socket程序中,遇到了这个问题,本以为是Android客户端程序的问题,没想到最后问题出现在服务端的Servlet中,也就是我们平时J2ee中经常遇到的405问题。
具体表现为:
Servlet是普通java写的,client是运行在android模拟器完成,现在情况是Android往Servlet发过去的数据,在Servlet中都可以得到,但是client端在接收Servlet响应的时候报错了!错误:request time failed: java.net.SocketException: Address family not supported by protocol
可以用这种方式做测试:
(我自己的机子:传了两个参数:username,password)
出现HTTP Status 405 - HTTP method GET is not supported by this URL
原因是:
1、继承自Httpservlet的Servlet没有重写对于请求和响应的处理方法:doGet或doPost等方法,默认调用父类的doGet或doPost等方法。 2、父类HttpServlet的doGet或doPost等方法覆盖了你写的到doGet或doPost等方法。 不管是1或2,父类HttpServlet的doGet或doPost等方法默认实现是返回状态码是405的Http错误表示 对于指定资源请求方法不被允许。 解决方法: 1、子类重写doGet或doPost等方法。 2、在你扩张的Servlet中重写doGet或doPost等方法来处理请求和响应时,不要调用父类的doGet或doPost等方法即去掉supper.doGet(request,response)和super.doPost(request,response);转载地址:http://uizmi.baihongyu.com/