Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

http tunneling: URLConnection.getOutputStream() Problem

hi

within an apllet/application written in JDK112, we have the following problem:

the getOutputStream() method failed with the following message (Exception.getMessage()) :

can't reset method : already connected

here is the code of our method write:

   protected void write(byte b[])
   {
         URLConnection connection;
         URL u=new URL("http://www.tratech.be:8080");
 
         try
         {  connection=u.openConnection();
         }
         catch(Exception e)
         {
            System.out.println("write: Could not open URL connection.");
            return;
         }
 
         connection.setDoOutput(true);

         try
         {  connection.connect();
         }
         catch (IOException e)
         {  System.out.println("Connect failed");
            return;
         }
 
>>       try
>>       {  output=connection.getOutputStream();
>>       }
>>       catch (Exception e)
>>       {  System.out.println("getOutputStream failed : " + e.getMessage());
>>          return;
>>       }
 
         output.write(b);
         output.flush();
         output.close();
   }

We tried with the connect() method, without. there was no difference.

Any idea?

thank you,

jerome Gonze
Tradeware Technology S.A.

jg@tratech.be

http://www.tratech.be
0
tv062397
Asked:
tv062397
  • 2
1 Solution
 
tv062397Author Commented:
Edited text of question
0
 
jpk041897Commented:
URLConnection is defined as an abstract class and is therfore unable to create an instance of itself. You can make the calls but since the object does not exist in memory, it will be unable to store values in its member variables.

Furthermore, abstract classes do not necesarily implement the methods they define, leaving this to lower level classses.

Think of abstract classes as a kind of template for lower level classes that might, or might not contain implementation details.

Suggestions:

1.- Use a lower level class, derrived from URLConnection (like URL).

2.- Create your own class that extends URLConnection and use that one instead and make sure that and implementation exists for the methods you plan to use via the source files. Otherwise you would have to write te implementations yourself.
0
 
tv062397Author Commented:


There is a GNU solution for this problem:

HTTPClient

this is a good documented package.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now