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(); }
public void ShellAsync(string e, Action<string> y) { #if AndroidShellAsync // http://www.android.pk/blog/general/launch-app-through-adb-shell/ // am start -a android.intent.action.MAIN -n com.android.settings/.Settings // am start tel:210-385-0098 // am start -a android.intent.action.CALL tel:245007 // am start -a android.intent.action.SENDTO "sms:5245007" -e "sms_body" "heyy" && input keyevent 22 && input keyevent 66 // am start -a android.intent.action.SENDTO -d sms:1234567890 --es sms_body ohai --ez exit_on_sent true // am start -a android.intent.action.SENDTO -d smsto:245007 --es sms_body ":*" --ez exit_on_sent true && am start -a android.intent.action.SENDTO -d sms:5245007 --es sms_body ":*" --ez exit_on_sent true && input keyevent 22 && input keyevent 66 // pm list packages // pm list packages -f //http://stackoverflow.com/questions/11201659/android-adb-shell-dumpsys-tool // am start -S -e sms_body 'your message body' \ //-e address receiver -t 'vnd.android-dir/mms-sms' \ //com.android.mms/com.android.mms.ui.ComposeMessageActivity \ //&& adb shell input keyevent 66 //am start -n com.google.android.youtube/.PlayerActivity -d http://www.youtube.com/watch?v=MTT-crZBB0k // http://stackoverflow.com/questions/7095470/android-read-send-text-messages-on-ubuntu // System.InvalidOperationException: Sequence contains more than one element //at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source) //at jsc.Languages.Java.JavaCompiler.GetArrayEnumeratorType() in x:\jsc.internal.svn\compiler\jsc\Languages\Java\JavaCompiler.overrride.cs:line 52 //at jsc.Languages.Java.JavaCompiler.GetImportTypes(Type t, Boolean bExcludeJavaLang) in x:\jsc.internal.svn\compiler\jsc\Languages\Java\JavaCompiler.WriteImportTypes.cs:line 363 //at jsc.Languages.Java.JavaCompiler.WriteImportTypes(Type ContextType) in x:\jsc.internal.svn\compiler\jsc\Languages\Java\JavaCompiler.WriteImportTypes.cs:line 22 //at jsc.Languages.Java.JavaCompiler.CompileType(Type z) in x:\jsc.internal.svn\compiler\jsc\Languages\Java\JavaCompiler.CompileType.cs:line 43 //at jsc.Languages.CompilerJob.<>c__DisplayClass1a.<CompileJava>b__17(Type xx) in x:\jsc.internal.svn\compiler\jsc\Languages\Java\CompilerJob.cs:line 120 // IsArrayEnumerator: ScriptCoreLib.Shared.BCLImplementation.System.__SZArrayEnumerator`1, ScriptCoreLibAndroid, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null //IsArrayEnumerator: ScriptCoreLib.Shared.BCLImplementation.System.__SZArrayEnumerator`1, ScriptCoreLibJava, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null try { // http://stackoverflow.com/questions/9062182/android-icmp-ping var p = new ProcessBuilder(new[] { "sh" }).redirectErrorStream(true).start(); var os = new DataOutputStream(p.getOutputStream()); //os.writeBytes(e + '\n'); os.writeBytes(e + "\n"); os.flush(); // Close the terminal os.writeBytes("exit\n"); os.flush(); // read ping replys var reader = new BufferedReader(new InputStreamReader(p.getInputStream())); string line = reader.readLine(); while (line != null) { y(line); line = reader.readLine(); } } catch (System.Exception ex) { y("AndroidShellAsync error: " + new { ex.Message }); } #elif ShellAsync try { var p = System.Diagnostics.Process.Start( new ProcessStartInfo("cmd") { //ex = {"The Process object must have the UseShellExecute property set to false in order to redirect IO streams."} UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true } ); y("pid: " + p.Id); y(""); //ex = {"Timeouts are not supported on this stream."} //p.StandardOutput.BaseStream.ReadTimeout = 4000; //p.StandardError.BaseStream.ReadTimeout = 4000; var StandardOutput = ""; var StandardError = ""; p.StandardInput.WriteLine(e); p.StandardInput.WriteLine("exit"); var ww = new AutoResetEvent(false); #region timeout var rr = new System.Threading.Thread( delegate() { StandardOutput = p.StandardOutput.ReadToEnd(); StandardError = p.StandardError.ReadToEnd(); ww.Set(); } ); rr.Start(); #endregion #region timeout new System.Threading.Thread( delegate() { System.Threading.Thread.Sleep(5000); if (rr.IsAlive) { rr.Abort(); } ww.Set(); //ex = {"Process must exit before requested information can be determined."} if (p.HasExited) return; p.Kill(); } ).Start(); #endregion System.Threading.Thread.Yield(); ww.WaitOne(); y(StandardOutput); y(StandardError); y(""); y("exit: " + p.ExitCode); } catch (System.Exception ex) { Debugger.Break(); } finally { y = null; } #else y("ShellAsync not implemented."); #endif }