简体   繁体   中英

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()? 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. 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. 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.

All of the previous methods implicitly call connect() and fire the request. So when you reach setDoOutput, the connection is already made.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM