我使用Handler请求访问一个站点。需要访问该站点文件并下载。该站点需要用户和密码,才能访问。用httpWebRequest输出Stream流引发System.NotSupportedException异常:{"This stream does not support seek operations."} 。StackTrace:at System.Net.ConnectStream.get_Length()
at demo.Code.AutoDownloadHandler.ProcessRequest(HttpContext context) in E:\demo.\Code\AutoDownloadHandler.cs:line 45 . 大家有没有解决这个异常的方法。期待大家的方案。
源码:
Code
GetResponseStream 的流不支持 Length
Stream responseStream = httpWebResponse.GetResponseStream();
BinaryReader binaryReader = new BinaryReader(responseStream);
byte[] arraryByte = new byte[responseStream.Length];
binaryReader.Read(arraryByte, 0, (int)responseStream.Length);
改为
MemoryStream stmMemory = new MemoryStream();
byte[] buffer = new byte[64 * 1024];
int i;
while ((i = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
stmMemory.Write(buffer, 0, i);
}
byte[] arraryByte = stmMemory.ToArray();
stmMemory.Close();
好用,非常感谢。
@yufeng217: 我通过arraryByte.Length实现 判断我是否要做下一步的处理, ths
byte[] bs = Encoding.UTF8.GetBytes(param);
参数param中含有特殊字符,没有转化成十六进制值的也会报这个错误。例如:特殊字符&,要转化成%26等