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();
}