Timeout for Blackberry HttpConnection

2019-02-16 00:35发布

问题:

In my project for Blackberry 4.5, I create HttpConnection via Connector.open. If I connect over MDS, I can specify ConnectionTimeout in additional params to my URL. How can I specify timeouts if using direct TCP connection or TCP over WiFi?

回答1:

According to this KB article, it's not possible to specify a connection timeout value for transports other than MDS.



回答2:

In some cases it's possible to use Http over Socket and SocketConnectionEnhanced with READ_TIMEOUT option:

public class HTTPSocketConnector
{

    static public String getHtml( String url, long timeout )
    {

        String response = "";
        try
        {
            String host = getHostUrl( url );
            String page = getPageUrl( url );
            SocketConnectionEnhanced hc = 
                ( SocketConnectionEnhanced )Connector.open( "socket://"
                    + host + ":80" );
            hc.setSocketOptionEx( SocketConnectionEnhanced.READ_TIMEOUT, 
                timeout );
            DataOutputStream dout = new DataOutputStream(
                    ( ( SocketConnection )hc ).openOutputStream() );
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            String request = "GET /" + page + " HTTP/1.1\r\n" + "Host: " 
                + host + ":80\r\n" + "User-Agent: MIDP2.0\r\n" 
                + "Content-Type: text/html\r\n\r\n";
            bos.write( request.getBytes() );
            dout.write( bos.toByteArray() );
            dout.flush();
            dout.close();            
            InputStream is = ( ( SocketConnection )hc ).openInputStream();
            byte[] bytes = null;            
            bytes = IOUtilities.streamToBytes( is );
            is.close();
            response = new String( bytes, "UTF-8" );
        }
        catch( Exception e )
        {
            response = e.getMessage();
        }
        return response;
    }

    private static String getPageUrl( String url )
    {
        String result = url;
        if( result.indexOf( "//" ) != -1 )
        {
            result = result.substring( result.indexOf( "//" ) 
                + "//".length(), result.length() );
        }

        if( result.indexOf( "/" ) != -1 )
        {
            result = result.substring( result.indexOf( "/" ) 
                + "/".length(), result.length() );
        }
        return result;
    }

    private static String getHostUrl( String url )
    {
        String result = url;

        if( result.indexOf( "//" ) != -1 )
        {
            result = result.substring( result.indexOf( "//" ) 
                + "//".length(), result.length() );
        }

        if( result.indexOf( "/" ) != -1 )
        {
            result = result.substring( 0, result.indexOf( "/" ) );
        }
        return result;
    }
}


回答3:

As per official document

When making a Transmission Control Protocol (TCP) connection from a BlackBerry smartphone, the default connection timeout is 2 minutes. This value takes into consideration the possible time it can take a BlackBerry smartphone to be granted access to send data on the wireless network, and for the connection to travel over the wireless network over the Internet to the destination server and back again. In some circumstances, this value is too long. When making a socket or Hypertext Transfer Protocol (HTTP) connection through the BlackBerry® Mobile Data System (BlackBerry MDS) Connection Service, it is possible to set the timeout value to a lower value than the value that is configured in the BlackBerry MDS Connection Service. By default, the value is 2 minutes. It is not possible to extend beyond the limit configured on the server. Use the ConnectionTimeout parameter to specify the timeout value. This parameter accepts a numerical value in milliseconds. The following is an example of an HTTP connection with a timeout value of 1 minute:

StreamConnection s = (StreamConnection)Connector.open("http://myserver.com/mypage.html;ConnectionTimeout=60000;deviceside=false"); HttpConnection httpConn = (HttpConnection)s;

Note: The ConnectionTimeout parameter is not supported by direct TCP connections or connections through a Wireless Application Protocol (WAP) gateway. Only TCP connections, made through the BlackBerry MDS Connection Service, support this parameter.

Please see the official BB link

http://supportforums.blackberry.com/t5/Java-Development/Control-the-connection-timeout-for-TCP-connections-through-the/ta-p/445851