Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

java.lang.IllegalStateException: Already connected

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 LiverailData.sendGet(
at Main.getData(
at Main.main(

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


private String ticket;
HttpsURLConnection con = null;
BufferedReader bufferedReader = null;
String url ="";
//Password is converted to MD5 Hash
String urlParameters ="";
public void sendPost() throws Exception {

System.out.println("LiverailLogin - Opening a secure connection...");
//String url = "";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setConnectTimeout( 30000 );
con.setReadTimeout( 30000 );
con.setRequestProperty( "Connection", "close" );
con.setRequestProperty("content-type", "application/xml");
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
if (con.getResponseCode()!= 200)
throw new RuntimeException("Failed : HTTP error code : " + con.getResponseCode());}
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) {


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

public String getTicket() {
return ticket;


class LiverailData {

public void sendGet(String ticket) throws Exception {

System.out.println("Liveraildata-->Retrieving data with valid token : "+ ticket);
String url = "";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
System.out.println("Liveraildata connections established-->" + con.getResponseMessage());

//add request header

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

//Check response status if != 200, than fail the connection
if (con.getResponseCode()!= 200)
throw new RuntimeException("Failed : HTTP error code : " + con.getResponseCode());}
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){

// writeToFile(inputLine);}

} catch (MalformedURLException e) {e.printStackTrace();}
Curse your sudden but inevitable betrayal! And this tiny ad too!
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
    Bookmark Topic Watch Topic
  • New Topic