Can anyone spot why this takes ~20 sec?

I am running the code below to post a JSON request to a local server 192.168.1.127.

curl -H "Content-type: application/json" -X POST

http:// 192.168.1.127:8080/bed -d

'{"command":{"value":3.012,"set":"target_pressure_voltage"},"id":2002,"side":"left","role":"command"}'

curl on the same box where the server is running is instant and the server does not complain.

A get request from the Android browser is fast. I have tried two Android devices with os version 4.x.

This question does not help as far as I can tell:

Android HttpURLConnection VERY slow

con.getInputStream() Takes ~20 sec:

String httpJson(String url, JSONObject job) {

String ret = null;

HttpURLConnection con = httpJsonCon(url);

if(con!=null)

httpJsonCon(con, url,job);

return ret;

HttpURLConnection mkCon(String url) {

HttpURLConnection con = null;

URL u = null;

try {

u = new URL(url);

con = (HttpURLConnection) (u.openConnection());

con.setRequestMethod("POST");

con.setUseCaches(false);

con.setRequestProperty("Content-Type", "application/json");

con.setRequestProperty("Accept", "text/plain");

con.setRequestProperty("Accept", "application/json");

con.setDoOutput(true);

con.setDoInput(true);

con.connect();

} catch (Exception e) {

Log.w(TAG, " e= " + e);

if(con!=null)

con.disconnect();

con = null;

return con;

String sendJson(HttpURLConnection con, JSONObject job) {

String ret = null;

if(con==null){

return ret;

try {

final String toWriteOut = job.toString();

final Writer out = new OutputStreamWriter(new BufferedOutputStream(

con.getOutputStream()), "UTF-8");

out.write(toWriteOut);

out.flush();

//con.getInputStream() Takes ~20 sec:

final BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));

} catch (IOException e) {

Log.d(TAG, " e= " + e);

ret = null;

} finally {

if(con!=null)

con.disconnect();

return ret;

Add a request header that specifies the post content length.

con.setRequestProperty("Content-Length", "" + json.length());

Can anyone spot why this takes ~20 sec?I am running the code below to post a JSON request to a local server 192.168.1.127.curl -H "Content-type: application/json" -X POSThttp:// 192.168.1.127:8080/bed... 阅读数 3.5K https(安全超文本传输协议)与http(超文本传输协议)相比,多了一层SSL认证,需要我们提供特定网点的证书才能访问 如果我们纯粹的用HttpsURLConnection去访问,则会报异常(使用不同的框架会导致所报的异常不同) 解决办法: 一、设置HttpsURLConnection,让它信任所有证书(即跳过验证步骤) 二、为Htt 其实问题很简单,就是很正常的 HttpURLConnection POST连接,当getResponseCode为 HttpURLConnection .HTTP_OK(200)和 HttpURLConnection .HTTP_CREATED(201)时没有任何问题。
jdk 自带 HttpsURLConnection 访问https 时好时坏  解决方案 private static class TrustAnyTrustManager implements X509TrustManager { public void checkClientTrusted(X509Certificate[] chain, String authTy
由于当时项目用的是http协议,然后要求说要改成https,所以遇到了一些问题,在此记录下。 1.第一个遇到的问题异常: java .security.cert.CertificateException:no name matching  网址 found。 由于https比http多了一些东西,其中就包括加密和证书的一些东西,所以当你在传输数据的时候,由于服务端的证书客户端无法信任,所以它无法...