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 ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

java.lang.IllegalStateException: Already connected

 
Greenhorn
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 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();}
}
 
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
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic