在我的黑莓4.5项目中,我通过Connector.open创建了HttpConnection。如果我通过MDS连接,我可以在URL的附加参数中指定ConnectionTimeout。如果使用直接TCP连接或WiFi上的TCP,如何指定超时?
发布于 2010-03-06 01:14:11
根据this KB article的说法,不可能为除MDS以外的传输指定连接超时值。
发布于 2010-03-09 22:28:52
在某些情况下,可以使用带有READ_TIMEOUT选项的Http over Socket和SocketConnectionEnhanced:
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;
}
}发布于 2013-05-29 19:54:06
根据官方文档提供的
从BlackBerry智能手机建立传输控制协议连接时,默认连接超时时间为2分钟。此值考虑了授予BlackBerry智能手机访问权限以在无线网络上发送数据的可能时间,以及连接通过无线网络通过互联网传输到目标服务器并返回的可能时间。在某些情况下,此值太长。通过BlackBerry®移动数据系统连接服务建立套接字或超文本传输协议连接时,可以将超时值设置为比BlackBerry®移动数据系统连接服务中配置的值更小的值。默认情况下,该值为2分钟。扩展不能超过服务器上配置的限制。使用ConnectionTimeout参数指定超时值。此参数接受以毫秒为单位的数值。以下是超时值为1分钟的HTTP连接示例:
StreamConnection s= (StreamConnection)Connector.open("http://myserver.com/mypage.html;ConnectionTimeout=60000;deviceside=false");HttpConnection httpConn = (HttpConnection)s;
注意:直接连接或通过无线应用程序协议(WAP)网关的连接不支持ConnectionTimeout参数。只有通过TCP连接服务建立的BlackBerry连接才支持此参数。
请查看官方BB链接
http://supportforums.blackberry.com/t5/Java-Development/Control-the-connection-timeout-for-TCP-connections-through-the/ta-p/445851
https://stackoverflow.com/questions/2387158
复制相似问题