java https客户端速度很慢

java https客户端速度很慢,java,https,client,Java,Https,Client,我正在尝试通过java访问web API API位于https服务器上 不幸的是,它工作得很慢 当我通过web浏览器访问API时, 它在1秒内给出响应 当我通过java访问API时, 它在3秒内给出响应 我做错了什么?我怎样才能更快地得到响应 我注意到慢的部分是这条线: HttpResponse response1=httpclient.execute(httpGet) 这是我的密码: static DefaultHttpClient httpclient ; static { Use

我正在尝试通过java访问web API

API位于https服务器上

不幸的是,它工作得很慢

当我通过web浏览器访问API时, 它在1秒内给出响应

当我通过java访问API时, 它在3秒内给出响应

我做错了什么?我怎样才能更快地得到响应

我注意到慢的部分是这条线: HttpResponse response1=httpclient.execute(httpGet)

这是我的密码:

static DefaultHttpClient httpclient ;

static {
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("LOGIN", "PASSWORD");
     httpclient = new DefaultHttpClient();

     HttpHost targetHost = new HttpHost("SERVER.org", 443, "https");

    httpclient.getCredentialsProvider().setCredentials(
            new AuthScope(targetHost.getHostName(), targetHost.getPort()),
           creds);

    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local
    // auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);

    // Add AuthCache to the execution context
    BasicHttpContext localcontext = new BasicHttpContext();
    localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);


  readXml("https://SERVER.org/PATH");
}



public static String readXml(String urlToRead){


    try {



        HttpGet httpGet = new HttpGet(urlToRead);

            HttpResponse response1 = httpclient.execute(httpGet);



        try {
            System.out.println(response1.getStatusLine());
            HttpEntity entity1 = response1.getEntity();



            BufferedReader in;
            String inputLine;
            String result="";
            try {

                in = new BufferedReader(new InputStreamReader(entity1.getContent(),"UTF-8"));
                while ((inputLine = in.readLine()) != null) {
                  result+=inputLine+"\n";
                }
                in.close();

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


        } finally {
            httpGet.releaseConnection();
        }
    } catch (ClientProtocolException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

返回null;}

我认为HTTPS握手很慢。没有理由在每个请求中使用新的DefaultHttpClient。您应该使用PoologClientConnectionManager来处理多http客户端

public Client(int maxConnectPerHost, int maxConnection, int connectTimeOut, int socketTimeOut,
        String cookiePolicy, boolean isAutoRetry, boolean redirect) {
    SSLContext sslcontext = null;
    try {
        sslcontext = SSLContext.getInstance("SSL");
        sslcontext.init(null, new TrustManager[] {
            new TrustAnyTrustManager()
        }, new java.security.SecureRandom());
    } catch (Exception e) {
        // throw something
    }
    // if a ssl certification is not correct, it will not throw any exceptions.
    Scheme https = new Scheme("https", 443, new SSLSocketFactory(sslcontext,
            SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER));
    Scheme http = new Scheme("http", 80, PlainSocketFactory.getSocketFactory());
    SchemeRegistry sr = new SchemeRegistry();
    sr.register(https);
    sr.register(http);

    connectionManager = new PoolingClientConnectionManager(sr, socketTimeOut, TimeUnit.SECONDS);
    connectionManager.setDefaultMaxPerRoute(maxConnectPerHost);
    connectionManager.setMaxTotal(maxConnection);
    HttpParams params = new BasicHttpParams();
    params.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, connectTimeOut);
    params.setParameter(ClientPNames.COOKIE_POLICY, cookiePolicy);
    params.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, redirect);
    params.setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, false);

    if (isAutoRetry) {
        client = new AutoRetryHttpClient(new DefaultHttpClient(connectionManager, params));
    } else {
        client = new DefaultHttpClient(connectionManager, params);
    }
}
然后使用客户端发送请求。客户机应该是单例的。这是doGet方法。并且不应释放连接

public HttpResponse doGet(String url, List<Header> headers)
            throws Exception {

    HttpGet get = new HttpGet(url);
    if (headers != null) {
        Header[] array = new Header[headers.size()];
        headers.toArray(array);
        get.setHeaders(array);
    }

    try {
        HttpResponse resp = client.execute(get);
        return resp;
    } catch (Exception e) {
        // throw something
    } 
}
握手会减慢您的应用程序。因此,您不应该在每个请求中关闭/释放连接。

尝试添加计时代码(调用System.currentTimeMillis())以确定哪些行花费的时间更长。上面的代码将在每次完成整个操作时都进行身份验证,但是在浏览器中,您可能会有一个会话来避免对每个请求进行身份验证。很高兴知道哪一行花费了所有的时间。“慢”部分是:HttpResponse response1=httpclient.execute(httpGet);
Client c = new Client(1024, 1024 * 1024, 10000, 10000, CookiePolicy.IGNORE_COOKIES, true, true);
HttpResponse r = null;
try {
    r = c.doGet("your url", null);
} finally{
    if (r != null) {
        EntityUtils.consumeQuietly(r.getEntity());
    }
}


// send request again. There is no https handshake in this time.
   try {
        r = c.doGet("your url", null);
    } finally{
        if (r != null) {
            EntityUtils.consumeQuietly(r.getEntity());
        }
    }