简体   繁体   English

线程“main”中的异常 java.lang.IllegalStateException:已连接

[英]Exception in thread “main” java.lang.IllegalStateException: Already connected

I'm trying to invoke a webservice call and get a response.我正在尝试调用网络服务调用并获得响应。 When I tried it first time it worked perfectly and printed the response.当我第一次尝试它时,它运行良好并打印了响应。 But after that one run, how many ever times I run it, i throws me但在那一跑之后,我跑了多少次,我把我扔了

Exception in thread "main" java.lang.IllegalStateException: Already connected
    at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(Unknown Source)
    at SOAPClient4XG.main(SOAPClient4XG.java:72)

I have tried various solutions provided for similar problem (like connect / disconnect) but nothing seems to make it work.我尝试了为类似问题提供的各种解决方案(如连接/断开连接),但似乎没有任何效果。 I understand that it tries to perform an operation on already existing connection, but not sure how to fix.我知道它尝试对现有连接执行操作,但不确定如何修复。 I'm fairly new to all this and I need help.我对这一切都很陌生,我需要帮助。

Below is my code下面是我的代码

    import java.io.*;
    import java.net.*;

    public class SOAPClient4XG 
    {
     private static HttpURLConnection httpConn;
     public static void main(String[] args) throws Exception {

     String SOAPUrl      = args[0];
     String xmlFile2Send = args[1];*/

     String SOAPUrl      = "http://10.153.219.88:8011/celg-svcs-soap/business/ApplicantEligibility";
     String xmlFile2Send = 
    "C:\\Users\\dkrishnamoorthy\\workspace\\SOAPUI_Automation\\src\\ApplicantElligibilty.xml";

          String SOAPAction = "";
        if (args.length  > 2) 
                SOAPAction = args[2];

        // Create the connection where we're going to send the file.
        URL url = new URL(SOAPUrl);
        URLConnection connection = url.openConnection();
        //URLConnection connection = new URLConnection(url);

        httpConn = (HttpURLConnection) connection;

        if(httpConn.getResponseCode()==500)
        {
            System.out.println("Error Stream for 500 : "+httpConn.getErrorStream());
        }

        // Open the input file. After we copy it to a byte array, we can see
        // how big it is so that we can set the HTTP Cotent-Length
        // property. (See complete e-mail below for more on this.)

        FileInputStream fin = new FileInputStream(xmlFile2Send);

        ByteArrayOutputStream bout = new ByteArrayOutputStream();

        // Copy the SOAP file to the open connection.
        copy(fin,bout);
        fin.close();

        byte[] b = bout.toByteArray();

        // Set the appropriate HTTP parameters.
        httpConn.setRequestProperty( "Content-Length",
                                     String.valueOf( b.length ) );
        httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
          httpConn.setRequestProperty("SOAPAction",SOAPAction);
        httpConn.setRequestMethod( "POST" );
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);

      //  httpConn.connect();

        // Everything's set up; send the XML that was read in to b.
        OutputStream out = httpConn.getOutputStream();
        out.write( b );    
        out.close();

        // Read the response and write it to standard out.

        InputStreamReader isr =
            new InputStreamReader(httpConn.getInputStream());
        BufferedReader in = new BufferedReader(isr);

        String inputLine;
        System.out.println("Printing the Response ");

        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);

        in.close();
    }

  public static void copy(InputStream in, OutputStream out) 
   throws IOException {


    synchronized (in) {
      synchronized (out) {

        byte[] buffer = new byte[256];
        while (true) {
          int bytesRead = in.read(buffer);
          if (bytesRead == -1) break;
          out.write(buffer, 0, bytesRead);
        }
      }
    }
  } 
}

Can you delay calling urlConnection.getResponseCode()? 您可以延迟调用urlConnection.getResponseCode()吗? That's where you problem is. 那就是你的问题所在。 Setting your request headers & request method before that call should fix it. 在该调用之前设置请求标头和请求方法应该可以解决该问题。

If you use eclipse version just restart it.如果您使用 eclipse 版本,只需重新启动它。 I met the same issue and I sorted out by doing that .我遇到了同样的问题,我通过这样做来解决。

I solved this because I had a forgotten watch for connection.getResponseCode() in my debugging interface in NetBeans.我解决了这个问题,因为我在 NetBeans 的调试界面中忘记了 connection.getResponseCode() 的监视。 Hope it might help others making the same mistake.希望它可以帮助其他犯同样错误的人。

If you have any watch relative to the response value of the request, such as getResponseCode(), getResponseMessage(), getInputStream() or even just connect(), you will get this error in debugging mode.如果您有任何与请求的响应值相关的监视,例如 getResponseCode()、getResponseMessage()、getInputStream() 甚至只是 connect(),您将在调试模式下收到此错误。

All of the previous methods implicitly call connect() and fire the request.前面的所有方法都隐式调用 connect() 并触发请求。 So when you reach setDoOutput, the connection is already made.所以当你到达 setDoOutput 时,连接已经建立。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 异常:java.lang.IllegalStateException:已连接 - Exception : java.lang.IllegalStateException: Already connected 使用Selenium的线程“main”java.lang.IllegalStateException中的异常 - Exception in thread “main” java.lang.IllegalStateException for using Selenium 线程“main”中的异常 java.lang.IllegalStateException:驱动程序可执行文件是一个目录 - Exception in thread “main” java.lang.IllegalStateException: The driver executable is a directory Android:java.lang.IllegalStateException:已经连接 - Android: java.lang.IllegalStateException: Already connected java.lang.IllegalStateException:已在setDoOutput处连接 - java.lang.IllegalStateException: Already connected at setDoOutput Android java.lang.IllegalStateException,不在主线程上 - Android java.lang.IllegalStateException, not on the main thread java.lang.IllegalStateException:不在主线程上 - java.lang.IllegalStateException: Not on the main thread java.lang.IllegalStateException:已连接(Discord JDA) - java.lang.IllegalStateException: Already connected(Discord JDA) 线程“主”java.lang.IllegalStateException 中的异常:驱动程序可执行文件必须存在错误,使用 Selenium 和 Java - Exception in thread "main" java.lang.IllegalStateException: The driver executable must exist error using Selenium and Java 无法为事务嵌套异常打开 JPA EntityManager 为 java.lang.IllegalStateException:已为键绑定到线程的值 - Could not open JPA EntityManager for transaction nested exception is java.lang.IllegalStateException:Already value for key bound to thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM