Blackberry HttpConnection 超时

发布于 2024-08-23 22:45:19 字数 152 浏览 2 评论 0 原文

在我的 Blackberry 4.5 项目中,我通过 Connector.open 创建 HttpConnection。如果我通过 MDS 连接,我可以在 URL 的附加参数中指定 ConnectionTimeout。如果使用直接 TCP 连接或 TCP over WiFi,如何指定超时?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

大姐,你呐 2024-08-30 22:45:19

根据这篇知识库文章,无法为 MDS 以外的传输指定连接超时值。

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

久随 2024-08-30 22:45:19

在某些情况下,可以使用 通过套接字的 HttpSocketConnectionEnhancedREAD_TIMEOUT 选项:

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;
    }
}

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;
    }
}
各空 2024-08-30 22:45:19

根据官方文档

从 BlackBerry 智能手机建立传输控制协议 (TCP) 连接时,默认连接超时为 2 分钟。该值考虑了 BlackBerry 智能手机被授予访问权限以在无线网络上发送数据以及连接通过无线网络通过 Internet 传输到目标服务器并再次返回所需的时间。在某些情况下,该值太长。通过 BlackBerry® Mobile Data System (BlackBerry MDS) Connection Service 建立套接字或超文本传输​​协议 (HTTP) 连接时,可以将超时值设置为低于 BlackBerry MDS Connection Service 中配置的值。默认情况下,该值为 2 分钟。无法超出服务器上配置的限制。使用 ConnectionTimeout 参数指定超时值。该参数接受以毫秒为单位的数值。以下是超时值为 1 分钟的 HTTP 连接示例:

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

注意:直接 TCP 连接或通过无线应用协议 (WAP) 网关的连接不支持 ConnectionTimeout 参数。只有通过 BlackBerry MDS Connection Service 建立的 TCP 连接支持此参数。

请参阅官方BB链接

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文