![](/img/trans.png)
[英]Exception : java.lang.IllegalStateException: Already connected
[英]Exception in thread “main” java.lang.IllegalStateException: Already connected
我正在尝试调用网络服务调用并获得响应。 当我第一次尝试它时,它运行良好并打印了响应。 但在那一跑之后,我跑了多少次,我把我扔了
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);
}
}
}
}
}
您可以延迟调用urlConnection.getResponseCode()吗? 那就是你的问题所在。 在该调用之前设置请求标头和请求方法应该可以解决该问题。
如果您使用 eclipse 版本,只需重新启动它。 我遇到了同样的问题,我通过这样做来解决。
我解决了这个问题,因为我在 NetBeans 的调试界面中忘记了 connection.getResponseCode() 的监视。 希望它可以帮助其他犯同样错误的人。
如果您有任何与请求的响应值相关的监视,例如 getResponseCode()、getResponseMessage()、getInputStream() 甚至只是 connect(),您将在调试模式下收到此错误。
前面的所有方法都隐式调用 connect() 并触发请求。 所以当你到达 setDoOutput 时,连接已经建立。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.