File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing HTTPS site using URL

 
Ashwin Philar
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 111
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic