?
Solved

HttpURLConnection

Posted on 2003-02-25
13
Medium Priority
?
852 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

762 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