Q) Accessing secure websites from a Java application : Java
This is a discussion on Q) Accessing secure websites from a Java application within the Java forums in Programming Languages category; I finally found the solution for this. As one poster pointed out, we need to use HttpClient.java Note that the following solution requires java 1.4 But you also need various other things. I have given them below: a) HttpClient.java -- I used commons-httpclient-2.0.jar b) commons-logging-api.jar c) EasySSLProtocolSocketFactory.java d) EasyX509TrustManager.java a) and b) can be found here: http://cvs.apache.org/builds/jakarta-commons/nightly/ c) and d) can be found here: http://cvs.apache.org/viewcvs.cgi/ja...t/contrib/ssl/ Here is a sample code that uses these to send https message to a server. Siva ----------- sample code begin ----------------- import javax.net.ssl.*; import java.net.*; import java.io.*; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.protocol.*; import org.apache.commons.httpclient.methods.*; public class ...
![]() |
| | LinkBack | Thread Tools |
|
#1
| |||
| |||
| need to use HttpClient.java Note that the following solution requires java 1.4 But you also need various other things. I have given them below: a) HttpClient.java -- I used commons-httpclient-2.0.jar b) commons-logging-api.jar c) EasySSLProtocolSocketFactory.java d) EasyX509TrustManager.java a) and b) can be found here: http://cvs.apache.org/builds/jakarta-commons/nightly/ c) and d) can be found here: http://cvs.apache.org/viewcvs.cgi/ja...t/contrib/ssl/ Here is a sample code that uses these to send https message to a server. Siva ----------- sample code begin ----------------- import javax.net.ssl.*; import java.net.*; import java.io.*; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.protocol.*; import org.apache.commons.httpclient.methods.*; public class HttpsTester{ private void process( String ip, String port, HttpClient httpclient) { String line = /* get the line to send to server */ GetMethod get = new GetMethod("/" + line); try{ httpclient.executeMethod(get); } catch (Exception e) { System.out.println("unable to execute request: " + e); return; } //output will be here byte[] responseBody = get.getResponseBody(); //process the output } public static void main(String [] args) { String ip = args[1]; String port = args[2]; int portNum = 0; try { portNum = Integer.parseInt(port); } catch (Exception e) { System.out.println("invalid port number -- must be numeric"); return; } //get a protocol object and specify which SSL protocol we // are going to use. //https indicates that we want to use SSL connection Protocol myhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), portNum); HttpClient httpclient = new HttpClient(); //specify ip address, port number and the protol to use httpclient.getHostConfiguration().setHost(ip, portNum, myhttps); HttpsTester tester = new HttpsTester(); System.out.println("connecting at ip = " + ip + " port = " + port); //read from a file and execute each line tester.process(ip, port, httpclient); } } ------------- sample code ends |
|
#2
| |||
| |||
| usenet@sta.samsung.com (Generic Usenet Account) wrote in message news:<90e5135.0405071047.7125b08f@posting.google.com>... > I finally found the solution for this. As one poster pointed out, we > need to use HttpClient.java > > Note that the following solution requires java 1.4 > > But you also need various other things. I have given them below: > > a) HttpClient.java -- I used commons-httpclient-2.0.jar > b) commons-logging-api.jar > c) EasySSLProtocolSocketFactory.java > d) EasyX509TrustManager.java > > a) and b) can be found here: > http://cvs.apache.org/builds/jakarta-commons/nightly/ > > c) and d) can be found here: > > http://cvs.apache.org/viewcvs.cgi/ja...t/contrib/ssl/ > > Here is a sample code that uses these to send https message to a > server. > > Siva I found an alternate solution to this problem in Beginning Java Networking by Alexander V. Konstantinou (Author) et al. Here it goes: ////////////////////////////////////////////////////////////////////////////// // URLGrab.java import java.io.*; import java.net.*; import javax.security.cert.Certificate; import com.sun.net.ssl.HttpsURLConnection; // J2SDK 1.3 // import java.net.ssl.HttpsURLConnection; // J2SDK 1.4 /** * A simple application that grabs the contents of an URL and dumps * them to a file. The URL may be a https URL. Some properties of the * connection are displayed to standard output. */ public class URLGrab { /** * The location of the https protocol handler */ private static final String PROTOCOL_HANDLERS = "com.sun.net.ssl.internal.www.protocol"; /** * Retrieves the contents and other information about a connection given * on the command line. * @param args the command line arguments * @throws Exception if something went wrong */ public static void main(String args[]) throws Exception { System.setProperty("java.protocol.handler.pkgs", PROTOCOL_HANDLERS); if (args.length != 2) { System.out.println("Please give an URL and a filename."); } else { URLConnection urlConn = new URL(args[0]).openConnection(); urlConn.connect(); // connect to the server displayProperties(urlConn); // display connection properties writeContents(urlConn, args[1]); // write URL contents to file } } /** * Writes the contents of the URL to a file * @param urlConn The connection * @param filename The name of the file to write to * @throws IOException if a network or other I/O error occurred */ private static void writeContents(URLConnection urlConn, String filename) throws IOException { InputStream in = urlConn.getInputStream(); OutputStream out = new FileOutputStream(filename); try { byte[] buffer = new byte[512]; int bytesRead; while ((bytesRead = in.read(buffer)) > 0) { out.write(buffer, 0, bytesRead); } } finally { try { out.close(); } catch (Exception e) { /* do nothing */ } try { in.close(); } catch (Exception e) { /* do nothing */ } } } /** * Displays some URL connection properties * @param urlConn The connection */ private static void displayProperties(URLConnection urlConn) { System.out.println("Content Length: " + urlConn.getContentLength()); System.out.println("Content Type: " + urlConn.getContentType()); System.out.println("Content Encoding: " + urlConn.getContentEncoding()); if (urlConn instanceof HttpsURLConnection) { displaySecureProperties((HttpsURLConnection) urlConn); } } /** * Displays some https URL connection properties * @param urlConn The secure connection */ private static void displaySecureProperties(HttpsURLConnection urlConn) { System.out.println("Cipher Suite: " + urlConn.getCipherSuite()); Certificate[] chain = urlConn.getServerCertificateChain(); for (int i = 0; chain != null && i < chain.length; i++) { System.out.println("Certificate #" + (i + 1) + ":\n" + chain[i]); } } } |
![]() |
| Thread Tools | |
| |
| ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Java program that reads websites to download from a file | usenet | Java | 1 | 11-19-2007 09:41 PM |
| Recommended Java Design Pattern Books/Websites? | usenet | Java | 4 | 08-28-2007 12:40 PM |
| MOSS2007: websites, shared services, application pools and all that? | usenet | Sharepoint | 1 | 06-20-2007 05:00 AM |
| how to restrict users accessing/browsing specific websites | usenet | Microsoft Exchange | 2 | 10-04-2006 10:45 AM |
| Host a secure web application and OWA, use as many servers and resources as necessary. | usenet | Inetserver | 5 | 05-10-2006 01:41 PM |



