?
Solved

HttpURLConnection

Posted on 2003-02-25
13
Medium Priority
?
860 Views
Last Modified: 2007-12-19
I have a problem with below code.
The program will hang when it reach connection.getInputStream() if the destination server has shutdown. How could I overcome this problem?

URL url = new URL(dest + "?" + tmpData.toString());
HttpURLConnection connection =
     (HttpURLConnection) url.openConnection();
               
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
                         
String inputLine;
StringBuffer result = new StringBuffer();

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

in.close();
0
Comment
Question by:peikfeng
13 Comments
 
LVL 3

Expert Comment

by:schybert
ID: 8023405
First of all you have to call connection.connect() before you can retrieve the InputStream. openConnection() only creates the HttpURLConnection in this case.

(And please don't keep hitting refresh on this question. Use the Reload question link instead.)
0
 

Author Comment

by:peikfeng
ID: 8023552
:">  my mistake for pressing refresh. I have posted request to delete those duplicated questions.

I have add in the connect() method, but it is still the same. I have simplified my program as follow

URL url = new URL(dest + "?" + tmpData.toString());
HttpURLConnection connection =
    (HttpURLConnection) url.openConnection();
connection.connect();
System.out.println("Waiting response...");
System.out.println("Response code >>> "+connection.getResponseCode());

the program can only reach "Waiting response..." and just hang there without timeout (I have been waiting for > 5 minutes) or exception.
0
 
LVL 3

Expert Comment

by:schybert
ID: 8023653
Are you sure it doesn't throw any Exception either?
0
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.

 

Author Comment

by:peikfeng
ID: 8023662
Yes, I was expecting to have at least "Connection refused"
But it just stop at "Waiting response...", nothing printed out.
0
 

Author Comment

by:peikfeng
ID: 8023666
Does this mean it has make send a request, but the server might stop in the middle and therefore the connection.getResponseCode() method still continue to wait for response?
0
 
LVL 3

Expert Comment

by:schybert
ID: 8023745
Hmmm... I'm guessing the server-side hangs somehow. Would you mind if someone else tried to contact the url to try it out?
0
 
LVL 1

Expert Comment

by:gkasinath
ID: 8023746
well, if in the process of sending a response, the server crashes, you will either get incomplete/incorrect data or the connection object will become null.
Check for the connection object being null, and you may get the answer.

Regds
Lupo
0
 
LVL 3

Expert Comment

by:schybert
ID: 8023801
Well, he would get a NullPointerException in that case and since no Exception is thrown it isn't null. If the server-side program doesn't even start to send data getResponseCode might hang... but I would have thought it would timeout...
0
 
LVL 3

Accepted Solution

by:
schybert earned 400 total points
ID: 8023856
I did some checking on the subject and it turns out that the reading methods of HttpURLConnection don't timeout. You'll have to either implement this with threads or use a third-party http package.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8024660
 peikfeng can you please ask a moderator to delete all of your duplicate questions?
0
 

Author Comment

by:peikfeng
ID: 8038669
girionis, I have already posted the request to delete all duplicate q, thx.

schybert, I'll try out using thread. Will update again later. Thx for the suggestion.
0
 

Expert Comment

by:hamilcar
ID: 8048518
Hi,

This is because the HttpUrlConnection surplied in the jdk, is in my opinion rather crappy (excuse my language)

There is no way to added a timeout, so if the client hangs in firewall... it will hang for a very long time..

My Suggestion is that you scrap the sun http connection and use another implementation..

The one i have used is httpClient, but there is also a execellent implemention at jakarta.apache.org under the commons project.

www.innovation.ch/java/HTTPClient
but be sure to read the dokumentation first.
especially this part :
http://www.innovation.ch/java/HTTPClient/FAQ.html#cookie_handler



/thomas
0
 

Author Comment

by:peikfeng
ID: 8055906
schybert, thread is a good work around since I dun want to use third party software. Thanks a lot.

hamilcar, thx for your suggestion too. I'll look into those packages you recommended. However, I'll have to give point to schybert.

Thanks again to everybody.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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:
Suggested Courses
Course of the Month9 days, 14 hours left to enroll

571 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