• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.IllegalStateException: Already connected

 
pepe kali
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a newbie to webservices so any help i get will be appreciated. I have createD a Main function that calls two of my Classes 1.liverailLogin and 2. liverailData. I can connect to the API server and responses with an XML file which contains a token via liverailLogin using HTTPS. Then via liverailData i connect again to API server via HTTP. In my liverailData class when i call this con.setRequestProperty("Ticket", ticket); my code fail with an error.Exception in thread "main" java.lang.IllegalStateException: Already connected
at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:3000)
at LiverailData.sendGet(liveraildata.java:81)
at Main.getData(main.java:38)
at Main.main(main.java:17)

I Have tried everything but cant seems to go any further! Can anyone help?

------------------1---LIVERAILLOGIN.JAVA-------------------

private String ticket;
HttpsURLConnection con = null;
BufferedReader bufferedReader = null;
String url ="https://api4.liverail.com/login";
//Password is converted to MD5 Hash
String urlParameters ="username=pippo@gmail.com&password=d372a23423242315b234714bd2523406708b4b234b9c46a35e";
public void sendPost() throws Exception {

System.out.println("LiverailLogin - Opening a secure connection...");
//String url = "https://api1.liverail.com/login";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setUseCaches(false);
con.setConnectTimeout( 30000 );
con.setReadTimeout( 30000 );
con.setRequestProperty( "Connection", "close" );
con.setRequestProperty("content-type", "application/xml");
con.setDoOutput(true);
con.setRequestMethod("POST");
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
if (con.getResponseCode()!= 200)
{
throw new RuntimeException("Failed : HTTP error code : " + con.getResponseCode());}
else
{
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);}

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine=null;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);

}
//in.close();
con.disconnect();

ticket = response.toString();
System.out.println("LiverainLogin : "+ response.toString());
}

public String getTicket() {
return ticket;
}


-------------------------2-----LIVERAILDAT.JAVA-------------------------------

class LiverailData {

public void sendGet(String ticket) throws Exception {

try{
System.out.println("Liveraildata-->Retrieving data with valid token : "+ ticket);
String url = "http://api4.liverail.com/entity/list";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setDoOutput(true);
System.out.println("Liveraildata connections established-->" + con.getResponseMessage());

//add request header

con.setRequestProperty("Ticket", ticket); // THE ERROR OCCURS HERE!
con.setRequestMethod("GET");

//Check response status if != 200, than fail the connection
if (con.getResponseCode()!= 200)
{
throw new RuntimeException("Failed : HTTP error code : " + con.getResponseCode());}
else
{
System.out.println("Liveraildata--> Content-Type :" + con.getContentType());
System.out.println("Liveraildata--> Ticket-->Success 200!");}

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));

String inputLine;

while ((inputLine = in.readLine()) != null){

System.out.println(inputLine);
// writeToFile(inputLine);}
//convertToCSV(inputLine);
}
in.close();
//con.disconnect();

} catch (MalformedURLException e) {e.printStackTrace();}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!