问题描述:服务器接收后台返回的报文时,提示java.lang.NegativeArraySizeException

分析:这种异常返回的原因,一般情况下没有报文提示为返回空报文,初步分析是响应报文流长度出了问题

百度一下类似的情况:https://stackoverflow.com/questions/11207897/negative-array-size-exception

节选部分内容:

try{
connection = (HttpConnection)Connector.open("http://someurl.xml",Connector.READ_WRITE);
URLEncodedPostData postData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false);
postData.append("username", "loginapi");
postData.append("password", "myapilogin");
postData.append("term", word); connection.setRequestMethod(HttpConnection.POST);
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
requestOut = connection.openOutputStream();
requestOut.write(postData.getBytes());
String contentType = connection.getHeaderField("Content-type");
detailIn = connection.openInputStream();
int length = (int) connection.getLength();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if(length > 0){//这里长度没有判定的情况下,byte array长度若为-1会产生错误
byte info[] = new byte[length];
int bytesRead = detailIn.read(info);
while(bytesRead > 0) {
baos.write(info, 0, bytesRead);
bytesRead = detailIn.read(info);
}
baos.close();
connection.close();
requestSuceeded(baos.toByteArray(), contentType); detailIn.read(info);
}
else
{
System.out.println("Negative array size");
}
requestOut.close();
detailIn.close();
connection.close();
}

  结论:HTTP服务器在返回响应报文的时候,没有进行content.length长度判断,按照常规流程响应了错误长度的报文,导致了接收方报文长度异常

最新文章

  1. webbrowser 内核切换
  2. linux svn 用户配置
  3. POJ 1724 ROADS【最短路/搜索/DP】
  4. .html 、.htm 、 .shtml 以及 .shtm 四种扩展名的文件区别
  5. 让UserControl能显示焦点状态
  6. OS X thrift setup
  7. iOS9 class dump header
  8. Oracle函数:求两个数的最大公约数
  9. unity3d 幻灯片效果实现
  10. 通过CreateOleObject控制IE
  11. Ubuntu常见问题
  12. 清除input[type=number]的默认样式
  13. .Net Core+Angular6 学习 第一部分(创建web api)
  14. CSS3选择器之:nth-child(n)
  15. 【JavaScript】脚本
  16. jQuery -- 如何使用jQuery判断某个元素是否存在
  17. nginx学习笔记(7)Nginx如何处理一个请求---转载
  18. intellij idea elixir 插件
  19. React Webpack cookbook
  20. Git 提示fatal: remote origin already exists

热门文章

  1. ACM学习历程—HDU1028 Ignatius and the Princess(组合数学)
  2. ACM学习历程—HDU1003 Max Sum(dp && 最大子序列和)
  3. python--面向对象(最全讲解)
  4. zabbix3.2部署
  5. MySQL锁之一:锁详解
  6. Jasper:SAOP API 函数
  7. std::ostringstream 转std::string
  8. function multi-versioning in GCC
  9. xgene:肿瘤相关基因 EGFR,,Her2,,TP53,,ALK
  10. CodeForces 484A Bits(水题)