public string UploadString(Uri u, string method, string data) { // http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/tip/src/share/classes/sun/net/www/protocol/http/HttpURLConnection.java // fails on openJDK why? Console.WriteLine("enter UploadString " + new { u, method }); var w = new StringBuilder(); HttpURLConnection xHttpURLConnection = null; try { #region NSA is that you? intercept? we can only trust pinned off device certs var trustAllCerts = new[] { new localX509TrustManager { } }; SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); HttpsURLConnection.setDefaultHostnameVerifier(new localHostnameVerifier { }); #endregion //UploadString getOutputStream //enter checkServerTrusted //enter getAcceptedIssuers //UploadString writeBytes var url = new java.net.URL(u.ToString()); xHttpURLConnection = (HttpURLConnection)url.openConnection(); var https = xHttpURLConnection as HttpsURLConnection; if (https != null) { //Console.WriteLine(new { https }); } //conn.setHostnameVerifier(new localHostnameVerifier { }); xHttpURLConnection.setDoOutput(true); xHttpURLConnection.setDoInput(true); xHttpURLConnection.setInstanceFollowRedirects(false); //conn.setInstanceFollowRedirects(true); xHttpURLConnection.setRequestMethod(method); var xContentType = default(string); try { if (Headers != null) { foreach (string key in Headers.AllKeys) { if (key == "Content-Type") { xContentType = Headers[key]; } xHttpURLConnection.addRequestProperty(key, Headers[key]); } } } catch (Exception e) { //System.Console.WriteLine("ERROR: Failed to write headers. Exception was:" + e.Message); } if (xContentType == null) { xHttpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); xHttpURLConnection.setRequestProperty("charset", "utf-8"); } //conn.setRequestProperty("content-length", "" + data.Length); xHttpURLConnection.setRequestProperty("Content-Length", "" + data.Length); xHttpURLConnection.setUseCaches(false); //Console.WriteLine("UploadString getOutputStream"); var o = xHttpURLConnection.getOutputStream(); //Console.WriteLine("UploadString writeBytes"); // DataOutputStream wr = new DataOutputStream(o); wr.writeBytes(data); //Console.WriteLine("UploadString flush"); wr.flush(); //Console.WriteLine("UploadString close"); wr.close(); //Console.WriteLine("UploadString readLine"); //var i = new java.io.InputStreamReader(url.openStream(), "UTF-8"); var i = new java.io.InputStreamReader(xHttpURLConnection.getInputStream(), "UTF-8"); var reader = new java.io.BufferedReader(i); // can't we just read to the end? var line = reader.readLine(); while (line != null) { w.AppendLine(line); line = reader.readLine(); } reader.close(); } catch (Exception err) { // 500 ? // = java.net.ProtocolException: Invalid HTTP method: // oops Console.WriteLine("UploadString " + new { err }); } //Console.WriteLine("exit UploadString " + new { conn }); if (xHttpURLConnection != null) { xHttpURLConnection.disconnect(); } return(w.ToString()); }
private SSLSocketFactory getSSLSocketFactory() { SSLSocketFactory factory = null; try { //reading the keyStore path and password from the environment properties string keyStorePath = java.lang.System.getProperty("javax.net.ssl.keyStore"); java.io.FileInputStream keyStoreStream = null; if (keyStorePath != null) { java.io.File file = new java.io.File(keyStorePath); if (file.exists()) { keyStoreStream = new java.io.FileInputStream(file); } else { keyStoreStream = searchDefaultCacerts(); } } else { keyStoreStream = searchDefaultCacerts(); } string keyStorePassWord = java.lang.System.getProperty("javax.net.ssl.keyStorePassword"); if (keyStorePassWord == null) { keyStorePassWord = "******"; } char[] passphrase = keyStorePassWord.ToCharArray(); //initiating SSLContext SSLContext ctx = SSLContext.getInstance("TLS"); KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); KeyStore ks = KeyStore.getInstance("JKS"); if (keyStoreStream != null) { ks.load(keyStoreStream, passphrase); } else { ks.load(null, null); } kmf.init(ks, passphrase); tmf.init(ks); ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); factory = ctx.getSocketFactory(); } catch (Exception e) { factory = null; #if DEBUG Console.WriteLine("Can't get SSL Socket Factory, the exception is {0}, {1}", e.GetType(), e.Message); #endif } return(factory); }