如何从GetresponseStream()中的流阅读器中读取byte []

时间:2012-04-24 07:40:41

标签: asp.net wcf http

我有一个WCF服务返回Byte [] data.how我可以在客户端读取这些数据

using GetrResponseStream();

HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
if (resp.StatusCode == HttpStatusCode.OK)
{
     Stream responseStream = resp.GetResponseStream();
     if (responseStream != null)
     {
         var reader = new StreamReader(responseStream, Encoding.UTF8);
         string dataString = reader.ReadToEnd();
     }
}

使用此代码获取

dataString ={"[255,34,56,778,3,54,6,8,9,90,0,87,6,.....]"} 

但我希望{WMS服务返回byte[],即我想要

byte[] dataString =[255,34,56,778,3,54,6,8,9,90,0,87,6,.....];

2 个答案:

答案 0 :(得分:4)

var memoryStream = new MemoryStream();
responseStream.CopyTo(memoryStream);
return memoryStream.ToArray();

答案 1 :(得分:0)

StreamReader主要用于阅读文字数据,您可以使用Stream课程GetBytebyte[]中阅读您的回复。请参阅以下代码:

HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
if (resp.StatusCode == HttpStatusCode.OK)
{
     using (Stream responseStream = resp.GetResponseStream())
     {
          /// you'll have your response in this variable
          var buffer = new byte[responseStream.Length];
          long bytesRemaining = responseStream.Length;
          int bytesRead = 0;

          while (bytesRemaining > 0)
          {
              int readCount = stream.Read(buffer, bytesRead, 1024);

              bytesRead += readCount;
              bytesRemaining -= readCount;
          }
     }
}