public static String accessNetworkByPost(String urlString, String contents) throws IOException {
DataOutputStream out = null;
URL postUrl;
String result = null;
// BufferedInputStream bis = null;
// ByteArrayBuffer baf = null;
HttpURLConnection connection = null;
try {
byte[] encrypted = contents.getBytes("utf-8");
postUrl = new URL(urlString);
connection = (HttpURLConnection) postUrl.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setConnectTimeout(10000);
connection.setReadTimeout(20000);
connection.setRequestMethod("POST");
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("contentType", "utf-8");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Content-Length", "" + encrypted.length);
out = new DataOutputStream(connection.getOutputStream());
out.write(encrypted);
out.flush();
out.close();
// bis = new BufferedInputStream(connection.getInputStream());
// baf = new ByteArrayBuffer(1024);
result=dealResponseResult(connection.getInputStream());
} catch (Exception e) {
e.printStackTrace();
Log.i("error", "NetworkUtil accessNetworkByPost exception:" + e.toString());
} finally {
if (connection != null) connection.disconnect();
// if (bis != null) bis.close();
// if (baf != null) baf.clear();
}
return result;
}
DataOutputStream out = null;
URL postUrl;
String result = null;
// BufferedInputStream bis = null;
// ByteArrayBuffer baf = null;
HttpURLConnection connection = null;
try {
byte[] encrypted = contents.getBytes("utf-8");
postUrl = new URL(urlString);
connection = (HttpURLConnection) postUrl.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setConnectTimeout(10000);
connection.setReadTimeout(20000);
connection.setRequestMethod("POST");
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("contentType", "utf-8");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Content-Length", "" + encrypted.length);
out = new DataOutputStream(connection.getOutputStream());
out.write(encrypted);
out.flush();
out.close();
// bis = new BufferedInputStream(connection.getInputStream());
// baf = new ByteArrayBuffer(1024);
result=dealResponseResult(connection.getInputStream());
} catch (Exception e) {
e.printStackTrace();
Log.i("error", "NetworkUtil accessNetworkByPost exception:" + e.toString());
} finally {
if (connection != null) connection.disconnect();
// if (bis != null) bis.close();
// if (baf != null) baf.clear();
}
return result;
}