在 url 对象中使用流

发布于 2025-01-01 01:19:29 字数 1012 浏览 0 评论 0原文

我正在使用客户端-服务器场景。客户端使用 url 连接与服务器(这是一个 servlet)进行通信。这是我正在使用的代码。

 URL url = new URL("http://localhost:8080/hello");
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);
    ObjectOutputStream out=new ObjectOutputStream(connection.getOutputStream());//1st out put stream
    out.writeObject(pk);
    out.flush();
    out.close();

    ObjectInputStream in = new ObjectInputStream(connection.getInputStream());//1st instream
    PublicKey spk=(PublicKey)in.readObject();
    in.close();

    ObjectOutputStream out1=new ObjectOutputStream(connection.getOutputStream());//2nd out put stream
    out1.writeObject(str1);
    out1.flush();
    out1.close();

    ObjectInputStream in1 = new ObjectInputStream(connection.getInputStream());      
    String rstr3=(String)in1.readObject();
    //processing 
    in1.close();

但我收到一个例外,称为:

java.net.ProtocolException:Cannot write output after reading input. 

我哪里出错了?

I am using a client-server scenario. The client communicates with the server(which is a servlet) using url connection. here is the code that i am using.

 URL url = new URL("http://localhost:8080/hello");
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);
    ObjectOutputStream out=new ObjectOutputStream(connection.getOutputStream());//1st out put stream
    out.writeObject(pk);
    out.flush();
    out.close();

    ObjectInputStream in = new ObjectInputStream(connection.getInputStream());//1st instream
    PublicKey spk=(PublicKey)in.readObject();
    in.close();

    ObjectOutputStream out1=new ObjectOutputStream(connection.getOutputStream());//2nd out put stream
    out1.writeObject(str1);
    out1.flush();
    out1.close();

    ObjectInputStream in1 = new ObjectInputStream(connection.getInputStream());      
    String rstr3=(String)in1.readObject();
    //processing 
    in1.close();

But i am getting an exception called:

java.net.ProtocolException:Cannot write output after reading input. 

Where am I going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟花肆意 2025-01-08 01:19:29

URLConnection 的实例不可重用:您必须为每个资源连接使用不同的实例。以下代码工作正常(打开一个新的 urlconnection 对象)

URLConnection connection1 = url.openConnection();
ObjectOutputStream out1=new ObjectOutputStream(connection1.getOutputStream());//2nd out put stream
    out1.writeObject(str1);
    out1.flush();
    out1.close();

    ObjectInputStream in1 = new ObjectInputStream(connection1.getInputStream());      
    String rstr3=(String)in1.readObject();
    //processing 
    in1.close();

Instances of URLConnection are not reusable: you must use a different instance for each connection to a resource. The following code works properly(open a new urlconnection object)

URLConnection connection1 = url.openConnection();
ObjectOutputStream out1=new ObjectOutputStream(connection1.getOutputStream());//2nd out put stream
    out1.writeObject(str1);
    out1.flush();
    out1.close();

    ObjectInputStream in1 = new ObjectInputStream(connection1.getInputStream());      
    String rstr3=(String)in1.readObject();
    //processing 
    in1.close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文