public static void main(String[] args) throws Exception { System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol"); /** * This test does not establish any connection to the specified URL, hence a dummy URL is used. */ URL foobar = new URL("https://example.com/"); HttpsURLConnection urlc = (HttpsURLConnection) foobar.openConnection(); try { urlc.getCipherSuite(); } catch (IllegalStateException e) { System.out.print("Caught proper exception: "); System.out.println(e.getMessage()); } try { urlc.getServerCertificateChain(); } catch (IllegalStateException e) { System.out.print("Caught proper exception: "); System.out.println(e.getMessage()); } try { urlc.setDefaultHostnameVerifier(null); } catch (IllegalArgumentException e) { System.out.print("Caught proper exception: "); System.out.println(e.getMessage()); } try { urlc.setHostnameVerifier(null); } catch (IllegalArgumentException e) { System.out.print("Caught proper exception: "); System.out.println(e.getMessage()); } try { urlc.setDefaultSSLSocketFactory(null); } catch (IllegalArgumentException e) { System.out.print("Caught proper exception: "); System.out.println(e.getMessage()); } try { urlc.setSSLSocketFactory(null); } catch (IllegalArgumentException e) { System.out.print("Caught proper exception"); System.out.println(e.getMessage()); } System.out.println("TESTS PASSED"); }
private String postMessage(URL url, String message) throws IOException { if (url == null) { throw new IOException("Bad URL : null"); } /* Contact the server. */ HttpURLConnection connection = null; String protocol = url.getProtocol().toLowerCase(); if (protocol.equals("https")) { HttpsURLConnection sslConnection = (HttpsURLConnection) url.openConnection(); sslConnection.setHostnameVerifier(new AutoVerifier()); connection = sslConnection; } else { connection = (HttpURLConnection) url.openConnection(); } connection.setDoOutput(true); connection.setDoInput(true); /* * if(requestContentType != null) * connection.setRequestProperty("Content-Type",requestContentType); * * if(setContentLength) * connection.setRequestProperty("Content-Length",Integer * .toString(message.length())); */ connection.setRequestMethod("POST"); /* * if(!Str.isEmpty(cookie)) { * connection.setRequestProperty("Cookie",cookie); * connection.setRequestProperty("Set-Cookie",cookie); * System.out.println("Setting cookie to " + cookie); } */ StringBuffer buffer = new StringBuffer(""); String agent = "Mozilla/4.0"; String rawData = "userid=joe&password=guessme"; String type = "application/x-www-form-urlencoded"; BufferedReader reader = null; // String encodedData = StringFunctions.urlEncoded( message ); // // user-supplied try { /* * connection = (HttpConnection) Connector.open( url ); * connection.setRequestMethod( HttpConnection.POST ); * connection.setRequestProperty( "User-Agent", agent ); * connection.setRequestProperty( "Content-Type", type ); * connection.setRequestProperty( "Content-Length", message.length() * ); // OutputStream os = conn.openOutputStream(); // os.write( * message.getBytes() ); */ PrintWriter writer = new PrintWriter(connection.getOutputStream()); writer.print(message); writer.close(); InputStream in = connection.getInputStream(); String replyContentType = connection.getContentType(); int replyContentLength = connection.getContentLength(); String cookie = connection.getHeaderField("set-cookie"); // DEBUG for (int count = 0; ; count++) { String key = connection.getHeaderFieldKey(count + 1); if (key == null) break; String val = connection.getHeaderField(count + 1); System.out.println("header field " + key + "=" + val); } reader = new BufferedReader(new InputStreamReader(in)); System.out.println("read input....."); String line; while ((line = reader.readLine()) != null) { System.out.println("in...." + line); buffer.append(line + "\n"); } } finally { try { if (reader != null) reader.close(); } catch (IOException e) { } } System.out.println("input....." + buffer.toString().trim()); return buffer.toString().trim(); }