Blackberry HTTPS连接
我使用以下代码建立Https连接Blackberry HTTPS连接,blackberry,blackberry-simulator,Blackberry,Blackberry Simulator,我使用以下代码建立Https连接 HttpsConnection httpConnector = null; InputStream in = null; Document doc ; String content = ""; try { httpConnector = (HttpsConnection)Connector.open(url,Connector.READ_WR
HttpsConnection httpConnector = null;
InputStream in = null;
Document doc ;
String content = "";
try
{
httpConnector = (HttpsConnection)Connector.open(url,Connector.READ_WRITE);
httpConnector.setRequestMethod(HttpConnection.GET) ;
in = httpConnector.openInputStream();
byte[] data = new byte[in.available()];
int len = 0;
int size = 0;
StringBuffer raw = new StringBuffer();
while ( -1 != (len = in.read(data)) ) {
raw.append(new String(data, 0, len));
size += len;
}
content = raw.toString().trim();
}
catch(Exception ex)
{
ex.printStackTrace();
return false;
}
try{
in.close();
in =null;
httpConnector.close();
httpConnector =null;
}catch(Exception ex)
{
Dialog.alert("Error:" + ex.getMessage());
return false;
}
}
我想我能够建立起联系,但价值观不会到来。我正在模拟器上测试它,我还没有在设备上测试我认为您的错误在以下方面:
byte[] data = new byte[in.available()];
available()
方法仅返回可立即从inputstream读取的字节数,但您正在使用它初始化临时字节数组的大小。由于available()可能返回0,因此您可能正在初始化一个零长度数组
使用固定长度的数组初始化“数据”会更好。我可以使用相同的代码使用httpconnection获取值。这可能只是时间的巧合,HTTP连接的available()值为正值,而HTTPS可能返回0。尝试用一个固定长度的字节数组替换它不会有什么坏处。我认为InputStream没有getLength(),我在.available()中替换了一些静态值,比如1024,但仍然没有显示任何内容。下面有几个问题:这是一个自签名SSL站点还是由可信CA(如Verisign)签名?它是否抛出任何异常?你能在真实的设备上试用吗?我能看到我用100000作为静态值的值,但问题是我不想保留静态值,所以这是另一个原因