在 url 对象中使用流
我正在使用客户端-服务器场景。客户端使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
URLConnection 的实例不可重用:您必须为每个资源连接使用不同的实例。以下代码工作正常(打开一个新的 urlconnection 对象)
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)