HttpURLConnection - Lots of requests

血红的双手。 提交于 2019-12-23 03:24:09

问题


I have written a service which is used to register some data. That service can be called thousands of times. The parameters are passed via the URL itself. The client part of it looks something like this:

private String getNodeInfoFromGPSForSpecificSubscriber( int subscriberId ) throws Exception {
        String requestURLToNode = "http://" + GPSConstants.GPS_IP + ":" + GPSConstants.PORT_NUMBER + APIs.FetchASpeecificNodeAPI.FETCH_A_SPECIFIC_NODE + "?" + APIs.FetchASpeecificNodeAPI.SUBSCRIBER_ID + "=" + subscriberId;
        URL url = new URL( requestURLToNode );
        HttpURLConnection conn = ( HttpURLConnection ) url.openConnection();
        conn.setRequestMethod( "GET" );
        conn.setRequestProperty( "Accept", "application/xml" );
        conn.setDoOutput( true );
        InputStream inputStream = conn.getInputStream();


        BufferedReader in = new BufferedReader( new InputStreamReader( inputStream ) );
        String responseAsString = in.readLine();
        in.close();

        conn.disconnect();
        System.out.println( ++i );

        return responseAsString;
    }

But here, every time a connection gets opened, and thus speed am getting is only around 1 record per second. I know I need not open a connection everytime, but not able to figure out how. Kindly help.

来源:https://stackoverflow.com/questions/23017813/httpurlconnection-lots-of-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!