Tomcat 错误:java.io.IOException:服务器返回 HTTP 响应代码:URL 的 405:

Posted

技术标签:

【中文标题】Tomcat 错误:java.io.IOException:服务器返回 HTTP 响应代码:URL 的 405:【英文标题】:Tomcat error: java.io.IOException: Server returned HTTP response code: 405 for URL: 【发布时间】:2013-08-12 07:57:34 【问题描述】:

使用Tomcat。我想向服务器发送一个字符串,让服务器操作该字符串,然后将其发回。写入输出流后,每当我尝试在客户端打开 InputStream 时,应用程序就会崩溃。

服务器:

public void doGet(HttpServletRequest request, 
     HttpServletResponse response) throws IOException, ServletException
    try

        ServletInputStream is = request.getInputStream();
        ObjectInputStream ois = new ObjectInputStream(is);

        String s = (String)ois.readObject();

        is.close();
        ois.close();

        ServletOutputStream os = response.getOutputStream(); 
        ObjectOutputStream oos = new ObjectOutputStream(os); 

        oos.writeObject("return: "+s);
        oos.flush();

        os.close();
        oos.close();

    
    catch(Exception e)

    

客户:

URLConnection c = new URL("*****************").openConnection();
c.setDoInput(true);
c.setDoOutput(true);
c.connect();

OutputStream os = c.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);

oos.writeObject("This is the send");
oos.flush();
oos.close();

InputStream is = c.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
System.out.println("ret: "+ois.readObject());

ois.close();
is.close();
os.close();

它返回此错误:

java.io.IOException: Server returned HTTP response code: 405 for URL: 
http://mywebpage.com

是什么导致了这个错误,我做错了什么?

【问题讨论】:

doGet and doPost String = null issue的可能重复 【参考方案1】:

您正在使用 HTTP servlet; Tomcat 作为 Web 服务器运行。您必须从客户端说 HTTP,而不是序列化的 Java 对象。如果要使用 Java 序列化,则需要使用套接字。如果您想使用 servlet,您需要使用某种形式将您的信息放入 HTTP; JSON(与 Jackson)是一个不错的选择。

【讨论】:

但是当我发送一个序列化对象时它可以工作,当我尝试接收一个并将它发送回同一个块时它只是崩溃。 虽然这可能不是 RPC 实现的模型,但没有理由不能交易序列化的 Java 对象。

以上是关于Tomcat 错误:java.io.IOException:服务器返回 HTTP 响应代码:URL 的 405:的主要内容,如果未能解决你的问题,请参考以下文章

平衡器在 hdfs HA 中不起作用

tomcat 的503错误

tomcat运行JSP出现500错误

tomcat 启动正常,网站确HTTP Status 500 错误

tomcat 的503错误 (接上篇)

tomcat 的503错误 (接上篇)