Solved

http tunneling: URLConnection.getOutputStream() Problem

Posted on 1997-06-23
3
333 Views
Last Modified: 2011-10-03
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
Comment
Question by:tv062397
  • 2
3 Comments
 

Author Comment

by:tv062397
ID: 1221939
Edited text of question
0
 
LVL 6

Accepted Solution

by:
jpk041897 earned 100 total points
ID: 1221940
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
 

Author Comment

by:tv062397
ID: 1221941


There is a GNU solution for this problem:

HTTPClient

this is a good documented package.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
java continue statement 10 72
Eclipse Neon and jdk 1.8.0 11 65
maven project error 5 19
Cipher Configuration on Apache HTTPD 4 16
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now