线程" main"中的例外情况java.lang.IllegalStateException:已经连接

时间:2014-11-24 18:49:47

标签: java url

我正在尝试调用webservice调用并获得响应。当我第一次尝试它时,它完美地工作并打印出响应。但在那次运行之后,有多少次我运行它,我抛出了我

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)

我尝试过针对类似问题提供的各种解决方案(例如连接/断开连接)但似乎没有任何解决方案可以使它工作。我知道它试图对已经存在的连接执行操作,但不确定如何修复。我对这一切都很陌生,我需要帮助。

以下是我的代码

    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);
        }
      }
    }
  } 
}

3 个答案:

答案 0 :(得分:0)

你能延迟调用urlConnection.getResponseCode()吗?那就是你遇到问题的地方。设置您的请求标题&请求方法在该调用之前应该修复它。

答案 1 :(得分:0)

如果您使用eclipse版本,请重新启动它。我遇到了同样的问题,并通过这样做整理出来。

答案 2 :(得分:-1)

我解决了这个问题,因为我在NetBeans的调试界面中忘记了对connection.getResponseCode()的监视。希望它可以帮助其他人犯同样的错误。

如果您有任何与请求的响应值相关的监视,例如getResponseCode(),getResponseMessage(),getInputStream()或什至只是connect(),您将在调试模式下收到此错误。

所有先前的方法都隐式调用connect()并触发请求。因此,当您到达setDoOutput时,连接已经建立。