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!

Problem Code. What is DashoA6275

843811Nov 7 2003 — edited Nov 7 2003
package com.htps;

import java.net.*;

import javax.net.*;
import javax.net.ssl.TrustManager;
import javax.net.ssl.*;
import java.security.cert.CRLException;
import java.io.*;
import java.util.Properties;
import com.sun.net.ssl.internal.ssl.*;
//import com.sun.net.ssl.internal.www.protocol.https.*;

import java.security.cert.Certificate;

public class HTTPSWM {

String httpserver;
public HTTPSWM(String server) {
httpserver = server;
}

public void HttpsConnect() {
// Create a trust manager that does not validate certificate chains like the default TrustManager
// This routine right is what IE do when they receive a certificate that is not their
// KeyStore.
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public boolean isServerTrusted(java.security.cert.X509Certificate[] t){
return true;
}
public boolean isClientTrusted(java.security.cert.X509Certificate[] t){
return true;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {}
}
};

try{
// create the factory where we can set some parameters for the connection
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
// Create the socket connection and open it to the secure remote web server
URL url = new URL(httpserver);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("Get");


// Once the connection is open to the remote server we have to replace the default HostnameVerifier
// with one of our own since we want the client to bypass the peer and submitted host checks even
// if they are not equal. If this routine were not here, then this client would claim that the submitted
// host and the peer host are not equal.
connection.setHostnameVerifier(new HostnameVerifier(){
public boolean verify(String rserver, SSLSession sses) {
if (!rserver.equals(sses.getPeerHost())){
System.out.println( "certificate <" + sses.getPeerHost() + "> does not match host <" + rserver + "> but " + "continuing anyway" );
}
return true;
}
public boolean verify(String t, String x){return true;}
});

// Make this URL connection available for input and output
//connection.connect();
connection.setDoOutput(true);

// Buffer input or output whenever you can because it has proven to be more efficient

// BufferedWriter out = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
out.write("GET / HTTP/1.0\n\n");
out.flush();
String line;
StringBuffer sb = new StringBuffer();
while((line = in.readLine()) != null) {
sb.append(line);
}
out.close();
in.close();
System.out.println(sb.toString());

Certificate[] certs = connection.getLocalCertificates();
for(int i = 0; i < certs.length; i++){
System.out.println(certs);
}

} catch (java.security.NoSuchAlgorithmException nsae) {
System.err.println("\n" + "The context specified does not exist. Check for the existence of JSSE" + "\n");
System.exit(-1);
} catch (java.security.KeyManagementException kme) {
kme.printStackTrace();
System.exit(-1);
} catch (java.net.MalformedURLException mue) {
System.err.println("\n" + "URL does not exist or protocol does not exist or there is a typo in the submitted URL" + "\n");
System.exit(-1);
} catch (java.net.UnknownHostException uhe) {
System.err.println("\n" + "Remote server does not exist in DNS." + "\n");
System.exit(-1);
} catch (java.io.IOException ioe) {
System.err.println("\n" + "I/O Exception in the connection ." + "\n");
ioe.printStackTrace();
System.exit(-1);
}
}

public static void main(String args[]) throws Exception {

if (args.length >= 1) {
HTTPSWM wms =new HTTPSWM(args[0]);
wms.HttpsConnect();
System.out.println("YES");
} else {
String mesg = "\n" + "You must supply a to connect:" + "\n" + "\n";
System.out.println(mesg);
System.exit(-1);
}
}
}


This is my Exception


I/O Exception in the connection .

java.net.ProtocolException: Invalid HTTP method: Get

at java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:232)

at sun.net.www.protocol.https.HttpsURLConnectionImpl.setRequestMethod(DashoA6275)

at com.htps.HTTPSWM.HttpsConnect(HTTPSWM.java:51)

at com.htps.HTTPSWM.main(HTTPSWM.java:115)


Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 5 2003
Added on Nov 7 2003
1 comment
242 views