如何为HttpWebResponse流使用streamReader(或类似的)

时间:2016-09-30 09:23:44

标签: c# xml streamreader httpwebresponse

我目前正在尝试检索xml响应并从Web服务解析结果,但是当我以流的形式接收响应并使用流式读取器读取结果并将xml响应转换为字符串时,我遇到了问题所以可以解析它以后得到一个结果。

测试时我收到路径中存在非法字符的错误,因为流读取器只接受文件路径而不是实际的xml响应对象本身。在广泛搜索了大多数类似帖子之后,大多数人都指向文本阅读器或xmlreaders,但是当HttpWebResponse返回一个我无法使用它们的流时。有没有人有想法或指示帮我克服这个?

我已经将代码片段包含在我处理响应的方式中。

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader responseStream = new StreamReader(response.GetResponseStream());


            string soapResult = responseStream.ReadToEnd();

以下是请求的代码,以防它有所帮助。

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Location);
            ASCIIEncoding encoding = new ASCIIEncoding();

            byte[] bytesToWrite = encoding.GetBytes(xml.ToString());
            request.Method = "POST";
            request.ContentLength = bytesToWrite.Length;
            request.ContentType = "application/soap+xml; charset=UTF-8";

            Stream newStream = request.GetRequestStream();
            newStream.Write(bytesToWrite, 0, bytesToWrite.Length);
            newStream.Close();

由于

0 个答案:

没有答案