-1

トムキャットを使用。サーバーに文字列を送信し、サーバーに文字列を操作させてから送り返したいと思います。出力ストリームに書き込んだ後、クライアントで入力ストリームを開こうとすると、アプリがクラッシュします。

サーバ:

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

このエラーの原因は何ですか、何が間違っていますか?

4

1 に答える 1

0

HTTP サーブレットを使用しています。Tomcat は Web サーバーとして実行されています。シリアル化された Java オブジェクトではなく、クライアントから HTTP を話す必要があります。Java シリアライゼーションを使用する場合は、ソケットを使用する必要があります。サーブレットを使用する場合は、何らかの形式で情報を HTTP に入れる必要があります。JSON (Jackson を使用) は適切な選択です。

于 2013-08-10T14:16:01.687 に答える