Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

HTTPS-Client

843811Aug 30 2002 — edited Sep 3 2002
Hi

Is there someone who can help me to solve this problem, I try to run this program to connect to a server using https (https:\\www.xxx.xxxxx.xx\servlet). To reach this server I have pass a proxy inside our network. Is this Exception related to proxy or is it some wrong coding in my program:

Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:232)
at sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:762)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(DashoA6275)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:510)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(DashoA6275)
at NetworkAccess.main(NetworkAccess.java:143)


Regards
rmoller1


import java.io.*;
import java.net.*;
import java.util.*;
import javax.net.ssl.*;
import java.security.*;


public class NetworkAccess
{
// Declare class variables
static URL myUrl = null;
static HttpsURLConnection hcu = null;
static FileInputStream fis = null;
static BufferedReader br = null;
static String lineRead = null;
static String content = new String("");
static String enc = new String("UTF-8");
static FileOutputStream fos = null;

public static void main(String[] args) throws Exception
{
//Set params for client identification
String CERTIFICATE_TYPE = "SunX509";
String KEYSTORE_TYPE = "JKS";
String SSL_PROTOCOL = "SSLv3";

//Name of the keystore files & passwords
String MYKEYSTORE = "xxxxxxxxxx.jks";
String MYKEYSTOREPASS = "xxxxxxxxxxx";
String MYTRUSTSTORE = "xxxxxxxxxx.jks";
String MYTRUSTSTOREPASS = "xxxxxxxx";

//Set up https to use SUN's internal ssl protocol and security provider
System.setProperty("java.protocol.handler.pkgs","javax.net.ssl");
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

//Use a proxy to access the internet.
//MARK: Remove these if no proxy...
System.setProperty("https.proxyHost","xxx.xx.xxx.xx");
System.setProperty("https.proxyPort","xxxx");

//Set up the DefaultSSLSocketFactory to use a supplied keystore for id + disable the trustmanager
try
{
//Get static objects
KeyManagerFactory kmf = KeyManagerFactory.getInstance(CERTIFICATE_TYPE);
TrustManagerFactory tmf = TrustManagerFactory.getInstance(CERTIFICATE_TYPE);
KeyStore ks = KeyStore.getInstance(KEYSTORE_TYPE);
SSLContext sslContext = SSLContext.getInstance(SSL_PROTOCOL);

//Load keystore from file
ks.load(new FileInputStream (MYKEYSTORE), MYKEYSTOREPASS.toCharArray());

//Inititate the KeyManagerFactory to use the keys + certificates from the loaded keystore
kmf.init(ks, MYKEYSTOREPASS.toCharArray());

//Load truststore from file
ks.load(new FileInputStream (MYTRUSTSTORE), MYTRUSTSTOREPASS.toCharArray());

//Inititate the TrustManagerFactory to use the certificates from the loaded keystore
tmf.init(ks);

//Initiate sslContext to use the created Keymanager[] and TrustManager[]
//For disabling server authentication, supply null for the TrustManager[]...
sslContext.init(kmf.getKeyManagers(),tmf.getTrustManagers(),new java.security.SecureRandom());

//At last , set the DefaultSSLSocketFactory to use the new SSLContext
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());

}
catch (Exception e)
{
System.out.println(e.toString());
System.exit(1);
}

// Check required arguments
if (args.length != 2)
{
System.err.println ("Syntax: Client <url> <xmldatafile>");
System.exit (1);
}

// Reading xmldata from file
try
{
fis = new FileInputStream(args[1]);
}
catch (FileNotFoundException e)
{
System.out.println("failed opening file!");
System.exit(0);
}

br = new BufferedReader(new InputStreamReader(fis));

while (( lineRead = br.readLine()) != null)
{
content = new String(content + lineRead + "\n");
}

br.close();

// Open URL-connection to the servlet class
try
{
myUrl = new URL(args[0]);
hcu = (HttpsURLConnection)myUrl.openConnection();


} catch (MalformedURLException e) { // new URL() failed

System.out.println("Could not create URL");
System.exit(0);

} catch (IOException e) { // openConnection() failed

System.out.println("Could not open connection to URL");
System.exit(0);
}

// Open a connection to write data to the servlet
hcu.setDoOutput(true);

String call = new String("&xmldata=" + URLEncoder.encode(content, enc));


PrintWriter out = new PrintWriter( hcu.getOutputStream());
out.println(call);
out.close();

// Open an buffered input stream
BufferedReader in = new BufferedReader( new InputStreamReader( hcu.getInputStream() ));

// Read input from the servlet
String inputLine;
while ((inputLine = in.readLine()) != null)
{
System.out.println(inputLine);
}
in.close();
}
}


Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 1 2002
Added on Aug 30 2002
1 comment
259 views