This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

BufferedReader.readLine() hangs when using SSL

 
Jon Morrey
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used a socket to connect to an HTTPS site (using JSSE 1.0.2 for the SSL). Everything almost works fine. I can open the socket, and can get an input and output stream, I can apparently write to the output stream. When I create a BufferedReader from the input stream and try to read from it, it just hangs.
Here's my code:
<blockquote>
<pre>
import java.io.*;
import java.net.*;
import java.security.*;

public class testSocket
{
public static void main(String[] args)
{
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
try
{
URL url = new URL(args[0]);
String protocol = url.getProtocol();
String host = url.getHost();
int port = url.getPort();
String file = url.getFile();
if((port == -1) && (protocol.equals("http")))
port = 80;
else if((port == -1) && (protocol.equals("https")))
port = 443;
Socket socket = new Socket(host, port);
InputStream contents = socket.getInputStream();
OutputStream rqst = socket.getOutputStream();
PrintWriter request = new PrintWriter(new OutputStreamWriter(rqst));
request.println("GET " + file);
request.flush();
BufferedReader inBuff = new BufferedReader(new InputStreamReader(contents));
String line;
while((line = inBuff.readLine()) != null) //hangs right here at inBuff.readLine()
System.out.println(line);
socket.close();
}
catch(Exception e)
{
System.err.println(e);
System.err.println("Bad URL");
}
}
}
</pre>
</blockquote>
From the command line this works:
java testConnect http://www.sun.com
But this hangs:
java testConnect https://www.sun.com

Can anyone give me a hand?

[This message has been edited by Jon Morrey (edited November 09, 2001).]
 
Jon Morrey
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured out on my own that I needed to use the SSLSocket class (part of the JSSE package) instead of the standard Socket class.
 
chris snyder
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've found that you can also use the HttpsURLConnection returned from a URL.openConnection() when using "HTTPS".
I am trying to figure out how to speed up the connection process as it is quite processor intensive to do the SSL "handshake".
Have you been able to reuse sessions and/or figured out the mechanism to do so?
Thanks,
Chris
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!