CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  Web Services

web service的缓冲区溢出????谁能解决?

楼主caomo(曹魔)2001-11-20 22:42:40 在 .NET技术 / Web Services 提问

在web   service函数中想对二进制文件编码,    
  [webmethod]    
                                  public   string   GetFile(string   filePath)    
                                  {FileStream   myfile=File.OpenRead(filePath);    
                                  BinaryReader   br=new   BinaryReader(myfile);    
                                  byte[]   btBuf=new   byte[myfile.Length];    
                                                  long   i=0;    
                                                  while   (br.PeekChar()>-1)    
                                                  {    
                                                                  btBuf[i]=br.ReadByte();    
                                                                  i++;    
                                                  }    
                                                                  myfile.Close();    
                                        return   System.Convert.ToBase64String(btBuf);    
                                    }    
  调用时当文件大于4k就出现:    
  未处理的“System.Web.Services.Protocols.SoapException”类型的异常出现在   syst    
  em.web.services.dll   中    
   
  附加信息:System.Web.Services.Protocols.SoapException:   服务器无法处理请求。    
  --->   System.ArgumentException:   转换缓冲区溢出。    
        at   System.Text.UTF8Encoding.GetChars(Byte[]   bytes,   Int32   byteIndex,   Int32    
    byteCount,   Char[]   chars,   Int32   charIndex,   UTF8Decoder   decoder)    
        at   System.Text.UTF8Decoder.GetChars(Byte[]   bytes,   Int32   byteIndex,   Int32    
  byteCount,   Char[]   chars,   Int32   charIndex)    
        at   System.IO.BinaryReader.InternalReadOneChar()    
        at   System.IO.BinaryReader.Read()    
        at   System.IO.BinaryReader.PeekChar()    
        at   useResData.FileServer.GetFile(String   filePath)   in   e:\ddisk\inetpub\www    
  root\sei\test\useresdata\fileserver.asmx.cs:line   125   问题点数:100、回复次数:10Top

1 楼itroad(软件民工)回复于 2001-11-20 22:54:53 得分 100

快加分!  
  请在你的web.config中加入:  
  <httpRuntime   executionTimeout="900"   maxRequestLength="102400"   />  
  其中的maxRequestLength="102400"就是你的app所能接受的最大文件kb数。  
  ok?Top

2 楼caomo(曹魔)回复于 2001-11-22 00:17:32 得分 0

不好意思,还是不能加分。我没有看到它起作用!  
  我又仔细测试了一下,这个错误不仅和大小有关而且还和文件类型有关,我处理一个4k的gif会出错,但一个30k的doc文件却没问题。好像对文本格式没问题,对二进制有问题:(  
  Top

3 楼itroad(软件民工)回复于 2001-11-22 10:11:14 得分 0

public   string   GetFile(string   filePath)    
                                  {FileStream   myfile=File.OpenRead(filePath);    
                                  BinaryReader   br=new   BinaryReader(myfile);    
                                  byte[]   btBuf=new   byte[myfile.Length];    
                                  //试一下  
                                  br.Read(byte,0,myfile.Length);  
                                  myfile.Close();    
                                      return   System.Convert.ToBase64String(btBuf);    
                                  }    
  Top

4 楼julyclyde(Java初学(大学不教只好自己学))回复于 2001-11-22 13:33:22 得分 0

这是不是个漏洞?能不能用来黑服务器?Top

5 楼caomo(曹魔)回复于 2001-11-22 18:20:31 得分 0

你试过了吗?  
  br.Read(byte,0,myfile.Length);  
  myfile.Length是long,br.Read中是int(强制转换会丢东西的),如果能这样我早就这样了,  
  何必绕着弯一个个字节读?  
  Top

6 楼caomo(曹魔)回复于 2001-11-22 18:21:59 得分 0

而且,这两种形式都是换汤不换药,只是一种简洁(但不可行:))  
  缓冲区损耗不会有丝毫改变。  
  Top

7 楼itroad(软件民工)回复于 2001-11-22 19:21:17 得分 0

[WebMethod]    
  public   string   GetFile(string   filePath)    
  {  
  System.IO.FileStream   myfile=System.IO.File.OpenRead(filePath);    
  System.IO.BinaryReader   br=new   System.IO.BinaryReader(myfile);    
  byte[]   btBuf=new   byte[myfile.Length];    
   
  myfile.Close();    
  return   System.Convert.ToBase64String(btBuf);    
  }    
  在我的机器上,运行正常。  
  注:我的文件是814kb,不过时间长了点。Top

8 楼caomo(曹魔)回复于 2001-11-23 23:41:06 得分 0

老大,你看了返回结果没,你的程序去掉了写缓冲区的部分。btBuf里面压根全都是空值啊。转换有何意义?Top

9 楼itroad(软件民工)回复于 2001-11-24 00:24:40 得分 0

我是故意的,实际上是否存在那句话与你的问题并无多大关系。  
  拿到new一个buffer就不分配缓冲区了吗?  
  关键的是我没有出现那种错误。  
   
  另外:加上那句话,我已经得到了正确的结果,不知你的状况如何?Top

10 楼caomo(曹魔)回复于 2001-11-25 14:44:39 得分 0

怪哉?  
  加上写缓冲区的动作,那么你就没对我的程序进行任何改动  
  难道真是  
  <httpRuntime   executionTimeout="900"   maxRequestLength="102400"   />  
  起了作用?你在你的web.config里加了这个吗?你试试不加这个会不会出问题?  
  你试过对一个>10k的gif或者其它纯二进制文件进行转换吗?  
  如果真的能解决问题我可以再给你些分。Top

相关问题

  • 缓冲区溢出,请教。
  • 是不是缓冲区溢出?
  • 关于缓冲区溢出的问题...
  • LINUX下的缓冲区溢出和WINDOWS下的缓冲区溢出都有什么区别?
  • LINUX下的缓冲区溢出和WINDOWS下的缓冲区溢出攻击都有什么区别?
  • 关于缓冲区溢出后的恶意代码
  • 为什么缓冲区溢出容易导致安全漏洞?
  • 使您的软件运行起来: 防止缓冲区溢出:
  • 能否详细得讲讲缓冲区溢出!
  • #######一个另我发疯的低级缓冲区溢出漏洞######

关键词

  • 二进制
  • 文件
  • 转换
  • btbuf
  • 缓冲区
  • myfile
  • binaryreader
  • filepath
  • tobase64
  • openread

得分解答快速导航

  • 帖主:caomo
  • itroad

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo