读取输入后无法写入输出

2022-09-03 09:25:39

我正在编写一个程序,该程序连接到servlet,这要归功于一个,但我在检查URL时卡住了HttpURLConnection

public void connect (String method) throws Exception {

server = (HttpURLConnection) url.openConnection ();
server.setDoInput (true);
server.setDoOutput (true);
server.setUseCaches (false);
server.setRequestMethod (method);
server.setRequestProperty ("Content-Type", "application / xml");

server.connect ();

/*if (server.getResponseCode () == 200)
{
System.out.println ("Connection OK at the url:" + url);
System.out.println ("------------------------------------------- ------- ");
}
else
System.out.println ("Connection failed"); 

}*/

我得到错误:

java.net.ProtocolException:读取输入后无法写入输出。

如果我用评论中的代码检查网址,但它在没有它的情况下工作得很好,不幸的是,我需要检查网址,所以我认为问题来自方法,但我不知道如何解决它getResponseCode

谢谢


答案 1

HTTP协议基于请求-响应模式:您首先发送请求,服务器响应。一旦服务器响应,您就无法再发送任何内容,这就没有意义了。(在知道您要发送的内容之前,服务器如何为您提供响应代码?

因此,当您调用 时,您有效地告诉服务器您的请求已完成,它可以处理它。如果要发送更多数据,则必须启动新请求。server.getResponseCode()

查看代码,您希望检查连接本身是否成功,但没有必要这样做:如果连接不成功,则 由 抛出 。但是,连接尝试的结果与 HTTP 响应代码不同,后者始终在服务器处理完所有输入之后出现。Exceptionserver.connect()


答案 2

我认为例外不是由于。应该有一些代码试图在读取响应后编写以设置请求正文。printing url

如果您尝试在获取后获取,则会发生此异常HttpURLConnection.getOutputStream()HttpURLConnection.getInputStream()

以下是 sun.net.www.protocol.http.HttpURLConnection.getOutputStream 的缩写:

public synchronized OutputStream getOutputStream() throws IOException {

     try {
         if (!doOutput) {
             throw new ProtocolException("cannot write to a URLConnection"
                            + " if doOutput=false - call setDoOutput(true)");
         }

         if (method.equals("GET")) {
             method = "POST"; // Backward compatibility
         }
         if (!"POST".equals(method) && !"PUT".equals(method) &&
             "http".equals(url.getProtocol())) {
             throw new ProtocolException("HTTP method " + method +
                                         " doesn't support output");
         }

         // if there's already an input stream open, throw an exception
         if (inputStream != null) {
             throw new ProtocolException("Cannot write output after reading 
                input.");
         }

         if (!checkReuseConnection())
             connect();

         /* REMIND: This exists to fix the HttpsURLConnection subclass.
          * Hotjava needs to run on JDK.FCS.  Do proper fix in subclass
          * for . and remove this.
          */

         if (streaming() && strOutputStream == null) {
             writeRequests();
         }
         ps = (PrintStream)http.getOutputStream();
         if (streaming()) {
             if (strOutputStream == null) {
                 if (fixedContentLength != -) {
                     strOutputStream = 
                        new StreamingOutputStream (ps, fixedContentLength);
                 } else if (chunkLength != -) {
                     strOutputStream = new StreamingOutputStream(
                         new ChunkedOutputStream (ps, chunkLength), -);
                 }
             }
             return strOutputStream;
         } else {
             if (poster == null) {
                 poster = new PosterOutputStream();
             }
             return poster;
         }
     } catch (RuntimeException e) {
         disconnectInternal();
         throw e;
     } catch (IOException e) {
         disconnectInternal();
         throw e;
     }
 }

推荐