通过https url连接,json webservice呼叫的响应时间过高

时间:2014-08-04 12:55:57

标签: java json httpsurlconnection

我正在使用https url连接将json内容发布到网址中,我也得到了回复。但令人失望的是,通话的响应时间约为2分钟。在分析相同的情况时,我发现延迟是第一次连接到服务器。 这是我正在使用的代码片段。有人能告诉我这个错了吗?

import java.net.*;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.io.*;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

import org.apache.axis2.java.security.TrustAllTrustManager;
import org.json.JSONObject;

public class httpsurl implements X509TrustManager {



public static void main(String s[]) throws ProtocolException {
    httpsurl.query("https://instance.service-now.com/incident.do?JSON");

}

public static void query(String URLName) {
    HttpsURLConnection con = null;
    try {

        JSONObject json = new JSONObject();
        json.put("parameter 1", "value 1");
        json.put("parameter 2", "value 2");



        sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
                "proxy name", 8080));
        String encodedUserPwd = encoder
                .encode("username:password".getBytes());
          URL u = new URL(URLName);
  //            System.setProperty("http.keepAlive", "false");

        con = (HttpsURLConnection) u.openConnection(proxy);
        SSLContext mySsl = SSLContext.getInstance("TLS");
         mySsl.init(null, new TrustManager[]{new TrustAllTrustManager()}, null);
        con.setRequestProperty("connection", "close");
        con.setReadTimeout(10000);
         con.setConnectTimeout(15000);
        con.setRequestMethod("POST");
         con.setDoInput(true);
        con.setDoOutput(true);
        con.addRequestProperty("Content-Type",  "application/json");
        con.setRequestProperty("Authorization", "Basic " + encodedUserPwd);
         con.setUseCaches(false);
        con.setSSLSocketFactory(mySsl.getSocketFactory());

//          con.connect();          

        OutputStream os = con.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                os, "UTF-8"));
        writer.write(json.toString());
          writer.flush();
        writer.close();
        os.close();



         // Get response
        int HttpsResult = con.getResponseCode();
        StringBuffer sb = new StringBuffer();
        if (HttpsResult == HttpsURLConnection.HTTP_OK) {
             BufferedReader br = new BufferedReader(new InputStreamReader(
                     con.getInputStream(), "utf-8"));
             String line = null;

             while ((line = br.readLine()) != null) {
                 sb.append(line + "\n");
             }
            br.close();

            System.out.println("" + sb.toString());
        } else {
            System.out.println(con.getResponseMessage());
        }




     } catch (MalformedURLException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (con != null) {
            con.disconnect();

        }
    }
}

@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1)
        throws CertificateException {
    // TODO Auto-generated method stub

}

@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1)
        throws CertificateException {
    // TODO Auto-generated method stub

}

@Override
public X509Certificate[] getAcceptedIssuers() {
    // TODO Auto-generated method stub
    return null;
}

}

提前致谢,

迪帕克

1 个答案:

答案 0 :(得分:2)

可能有多种错误。没有更多的故障排除,很难说......根据我的经验,它可以是以下任何一种:

  1. 服务器端问题(呼叫确实需要很长时间) - 无论你是否使用http / https(https会稍慢),响应速度都会很慢。
  2. DNS查询时间(这会影响http / https)。
  3. 防火墙问题(例如,执行DNS请求,或者主机名有多个A记录,有些被阻止)。
  4. Cookie在请求/响应中发送,标记为安全,服务器没有良好的熵源(例如:http://theheat.dk/blog/?p=1539
  5. 我发现您使用的是TrustAllManager。在过去,我遇到过类似问题。最好将自签名证书导入您的信任存储区。它看起来效果更好。 (有关如何执行此操作的示例,请参阅http://www.chrissearle.org/2007/10/25/Adding_self-signed_https_certificates_to_java_keystore/。)
  6. 诊断这是最好的选择是Wireshark。如果您可以使用wireshark / tcpdump显示您的代码在执行con.getOutputStream()后立即启动网络连接并且SYn / SYNC + ACK / ACK快速发生,则可以将手指指向服务器。

    祝你好运!