aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Accessing HTTPS site using URL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Accessing HTTPS site using URL" Watch "Accessing HTTPS site using URL" New topic
Author

Accessing HTTPS site using URL

Ashwin Philar
Greenhorn

Joined: Dec 11, 2001
Posts: 5
Hi!!
I have jdk1.3.1_01 on my machine and this is how my classpath variable looks like:
jcert.jar;jnet.jar;jsse.jar
This is my code:
import java.net.*;
import java.io.*;
import java.security.*;
public class URLReaderorg
{
public static void main(String[] args) throws Exception
{
// create a new protocol handler
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
// protocol handler uses this security provider
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

URL yahoo = new URL("https://ibank.amsouth.com/");

BufferedReader in = new BufferedReader(new InputStreamReader(yahoo.openStream()));
String inputLine;
File outputFile = new File("output.html");
FileWriter out = new FileWriter(outputFile);
while ((inputLine = in.readLine()) != null)
{
out.write(inputLine);
out.write('\n');
}
in.close();
out.close();
}
}
This is the error I am getting:
URLReaderorg.java:14: cannot resolve symbol
symbol : class Provider
location: package ssl
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()
);
^
1 error
Please help!!

Thanks
Ashwin Philar
Laudney Ren
Ranch Hand

Joined: Jan 06, 2002
Posts: 111
Java Secure SocketExtension contains 4 packages:
javax.net.ssl, javaxj.net, javax.security.cert,com.sun.net.ssl.
Sun offers the extension in a zip file. After unzipping it, under the directory lib, you find three jar files: jcert.jar,jnet.jar, jsse.jar. They should be put in the CLASSPATH. Or with jdk1.2 and later, simply put them under jre/lib/ext.
Here is my implementation code. I add an import from javax.net.ssl.* and uses SSLSocket.
import java.net.*;
import java.io.*;
import java.security.*;
import javax.net.ssl.*;
public class HTTPSClient {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java HTTPSClient host");
return;
}
int port = 443; //default https port
String host = args[0];
try {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket socket = (SSLSocket) factory.createSocket(host,port);
Writer out = new OutputStreamWriter(socket.getOutputStream());
//https requires GET contains full URL
out.write("GET http://" + host + "/ HTTP 1.1\r\n");
out.write("\r\n");
out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
int c;
While ((c = in.read) != -1) {
System.out.write(c);
}
out.close();
in.close();
socket.close();
}
catch (IOException e) {
System.err.println(e);
}
}
}


" Veni, vidi, vici "<br />" I came, I saw, I conquered "
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing HTTPS site using URL