流不可写
Stream was not writable
我正在尝试从 Web SOAP Api 读取响应,并在 foreach 循环中的第二个 运行 上收到 Stream was not writable
错误。 list ArrayList
中的第一个值按预期工作,但第二个值表示 Stream was not writable
行上的 Stream was not writable
。我做错了什么?
WebRequest webRequest = WebRequest.Create("https://www.api.com/v2.0/xml/");
HttpWebRequest httpRequest = (HttpWebRequest)webRequest;
httpRequest.Method = "POST";
httpRequest.ContentType = "text/xml; charset=utf-8";
httpRequest.ProtocolVersion = HttpVersion.Version11;
httpRequest.Credentials = CredentialCache.DefaultCredentials;
Stream requestStream = httpRequest.GetRequestStream();
StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);
StringBuilder soapRequest;
foreach (var bol in list) {
oRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
oRequest = oRequest + "<file>";
oRequest = oRequest + "<IntDocNumber>" + bol + "</IntDocNumber>";
oRequest = oRequest + "</file>";
soapRequest = new StringBuilder(oRequest);
streamWriter = new StreamWriter(requestStream, Encoding.ASCII);
streamWriter.Write(soapRequest.ToString());
streamWriter.Close();
//Get the Response
HttpWebResponse wr = (HttpWebResponse)httpRequest.GetResponse();
StreamReader srd = new StreamReader(wr.GetResponseStream());
string resulXmlFromWebService = srd.ReadToEnd();
XmlDocument xml = new XmlDocument();
xml.LoadXml(resulXmlFromWebService);
XmlNodeList xnList = xml.SelectNodes("/root/data/item");
}
所有 HTTP 通信都分为三个步骤:
- 客户端打开一个连接
- 客户端发送其 headers 和数据(在您的情况下,XML 文档)并结束输入(在您关闭请求流的情况下)
- 服务器发送响应并关闭连接
因此,当您关闭连接时,您将无法发送任何其他内容,因为您必须等待服务器应答。如果您想发送更多数据,请将代码的开头(您创建新 WebRequest 的位置)移到 For 循环中。
我正在尝试从 Web SOAP Api 读取响应,并在 foreach 循环中的第二个 运行 上收到 Stream was not writable
错误。 list ArrayList
中的第一个值按预期工作,但第二个值表示 Stream was not writable
行上的 Stream was not writable
。我做错了什么?
WebRequest webRequest = WebRequest.Create("https://www.api.com/v2.0/xml/");
HttpWebRequest httpRequest = (HttpWebRequest)webRequest;
httpRequest.Method = "POST";
httpRequest.ContentType = "text/xml; charset=utf-8";
httpRequest.ProtocolVersion = HttpVersion.Version11;
httpRequest.Credentials = CredentialCache.DefaultCredentials;
Stream requestStream = httpRequest.GetRequestStream();
StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);
StringBuilder soapRequest;
foreach (var bol in list) {
oRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
oRequest = oRequest + "<file>";
oRequest = oRequest + "<IntDocNumber>" + bol + "</IntDocNumber>";
oRequest = oRequest + "</file>";
soapRequest = new StringBuilder(oRequest);
streamWriter = new StreamWriter(requestStream, Encoding.ASCII);
streamWriter.Write(soapRequest.ToString());
streamWriter.Close();
//Get the Response
HttpWebResponse wr = (HttpWebResponse)httpRequest.GetResponse();
StreamReader srd = new StreamReader(wr.GetResponseStream());
string resulXmlFromWebService = srd.ReadToEnd();
XmlDocument xml = new XmlDocument();
xml.LoadXml(resulXmlFromWebService);
XmlNodeList xnList = xml.SelectNodes("/root/data/item");
}
所有 HTTP 通信都分为三个步骤:
- 客户端打开一个连接
- 客户端发送其 headers 和数据(在您的情况下,XML 文档)并结束输入(在您关闭请求流的情况下)
- 服务器发送响应并关闭连接
因此,当您关闭连接时,您将无法发送任何其他内容,因为您必须等待服务器应答。如果您想发送更多数据,请将代码的开头(您创建新 WebRequest 的位置)移到 For 循环中。