https versus http using Java - delays

R

Robert Mazur

I'm looking for some general guidance regarding https connection versus
http using Java. Might be some good puzzles for some of you. I have
tried this requesting two servers in two entirely different domains,
from two different locations. Exact same results. I am using jsse 1.0.3.

#1-DELAY) I have some code that used to fetch data from a web server
via https in about 5-7 seconds. I haven't used the code for 8 months,
and now when coming back to it I find that a round-trip connection (ie.
getting some data back) with either of two servers takes 25-30 seconds.
The same code using just http is instant, as I would expect. Why is
my getting OutputStream taking forever (see below)?

#2-JDK) My code works while using jsdk1.3. But if I switch to
jsdk1.4.2 (still using jsse1.0.3), I get a "java.net.ConnectException:
Connection timed out: connect" when connecting to the servers via https.
It's the same configuaration, as I am just asking Eclipse to switch
JDK's. Something must be different in the java.net.* package. But what?


// I pulled out all the try-catches to make more readable for now
HttpURLConnection connection;
URL url;

// Create new URL and connect
java.security.Security.addProvider(
new com.sun.net.ssl.internal.ssl.Provider());
System.getProperties().put(
"java.protocol.handler.pkgs",
"com.sun.net.ssl.internal.www.protocol");
url = new URL("https://www.somevaliddomain.com");
connection = (HttpsURLConnection) url.openConnection();

connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
if (this.encodedPass != null)
connection.setRequestProperty(
"Proxy-Authorization", this.encodedPass);

// SEND REQUEST TO SERVER
String queryString = "valid_request_for https_server";
//NEXT LINE TAKES AVE OF 20 SECONDS with jdk1.3
//or fails using jsdk1.4.2
OutputStream out = connection.getOutputStream();
out.write(queryString.getBytes());
out.close();

// READ THE ANSWER
String data = readURLConnection(connection);
 
A

Andrew Thompson

On Sun, 14 Nov 2004 08:30:41 -0800, Robert Mazur wrote:

(snip)
String data = readURLConnection(connection);

What's this 'readURLConnection' method?

You are more likely to get help if you post an SSCCE*.
<http://www.physci.org/codes/sscce.jsp>

* In this case, you cannot do a 'proper' SSCCE, since nobody
(I presume) wants you to include the JSSE, but it should be
complete beyond that.
 
R

Robert Mazur

Andrew said:
On Sun, 14 Nov 2004 08:30:41 -0800, Robert Mazur wrote:

(snip)



What's this 'readURLConnection' method?

You are more likely to get help if you post an SSCCE*.
<http://www.physci.org/codes/sscce.jsp>

* In this case, you cannot do a 'proper' SSCCE, since nobody
(I presume) wants you to include the JSSE, but it should be
complete beyond that.

Thanks for the response Andrew. Sorry I neglected to note what that
method does. It simply reads the characters out of the response from
the server. I should note that after this line:
OutputStream out = connection.getOutputStream();

....the rest of the code executes in a second or so using jdk1.3
(excellent). So I think the readURLConnection is fine. With jskd1.4.2,
I get a java.net.ConnectException on the getOutputStream() call, and of
course nothing further happens.

Thanks!
Rob

//---------------------------------------------
private static String readURLConnection(URLConnection uc)
throws Exception
{
StringBuffer buffer = new StringBuffer();
BufferedReader reader = null;
try
{
reader = new BufferedReader(new
InputStreamReader(uc.getInputStream()));
int letter = 0;
while ((letter = reader.read()) != -1)
buffer.append((char) letter);
}
catch (Exception e)
{
System.out.println("Cannot read from URL" + e.toString());
throw e;
}
finally
{
try
{ reader.close(); }
catch (IOException io)
{
System.out.println("Error closing URLReader!");
throw io;
}
}
return buffer.toString();
}
 
R

Robert Mazur

Andrew said:
On Sun, 14 Nov 2004 08:30:41 -0800, Robert Mazur wrote:

(snip)



What's this 'readURLConnection' method?

You are more likely to get help if you post an SSCCE*.
<http://www.physci.org/codes/sscce.jsp>

* In this case, you cannot do a 'proper' SSCCE, since nobody
(I presume) wants you to include the JSSE, but it should be
complete beyond that.

Andrew,

Thanks for your suggestion of building a SSCCE. Upon doing so (I was
going to repost a full working example) I found this code worked without
trouble, on either JDK. Clearly my problem is outside this block.
Though I am baffled why my other parts aren't working, I am relieved
that this part of the issue is resolved.

I probably wouldn't have know this without your SSCCE suggestion. :)
I will debug elsewhere.

Thanks!
Rob
 
S

Sudsy

Robert Mazur wrote:
I probably wouldn't have know this without your SSCCE suggestion. :) I
will debug elsewhere.

Good for you, Robert! Reducing a problem to the essentials often results
in interesting discoveries. Now you just have to work your way out to
learn where the bottleneck actually resides. Andrew's suggestion wasn't
so silly after all, eh?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top