.net 如何在 c# 中读取 httpWebRequest 的请求流,我收到错误“流不可读”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2263955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to read httpWebRequest's request stream in c#, i got error" the stream is not readable"?
提问by sam
I want to read the request stream from a custom HttpWebRequestclass that inherits from HttpWebRequestand I have tried to read the request stream in different stages but still not sure how to achieve that in the class.
我想从HttpWebRequest继承自的自定义类读取请求流HttpWebRequest,我尝试在不同阶段读取请求流,但仍然不确定如何在类中实现。
This custom HttpWebRequestis used to serialize a soap message and I want to know what request has been sent in string format. I also implemented custom HttpRequestCreator, HttpWebResponsebut still can't find a place/stage from which I can read the request stream.
此自定义HttpWebRequest用于序列化soap消息,我想知道以字符串格式发送了什么请求。我也实现了 custom HttpRequestCreator,HttpWebResponse但仍然找不到可以读取请求流的地方/阶段。
If i output everything in a MemoryStreamthen copy the content to request stream, anyone knows which stage I can do it? In the constructor, BeginGetRequestStream, EndGetRequestStreamor GetRequestStream?
如果我在 a 中输出所有内容,MemoryStream然后将内容复制到请求流,有人知道我可以做到哪个阶段吗?在构造函数中,BeginGetRequestStream,EndGetRequestStream或GetRequestStream?
回答by Cheeso
The "stream is not readable" will result if there is an error return code, like 404, 503, 401, and so on. It's likely you haven't checked your status code.
如果存在错误返回码,如 404、503、401 等,则会导致“流不可读”。您可能没有检查状态代码。
Something like this works if the content is text:
如果内容是文本,这样的事情会起作用:
public string DownloadString(string uri, out int status)
{
string result= null;
status = 0;
HttpWebResponse response= null;
try
{
HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(uri);
// augment the request here: headers (Referer, User-Agent, etc)
// CookieContainer, Accept, etc.
response= (HttpWebResponse) request.GetResponse();
Encoding responseEncoding = Encoding.GetEncoding(response.CharacterSet);
using (StreamReader sr = new StreamReader(response.GetResponseStream(), responseEncoding))
{
result = sr.ReadToEnd();
}
status = (int) response.StatusCode;
}
catch (WebException wexc1)
{
// any statusCode other than 200 gets caught here
if(wexc1.Status == WebExceptionStatus.ProtocolError)
{
// can also get the decription:
// ((HttpWebResponse)wexc1.Response).StatusDescription;
status = (int) ((HttpWebResponse)wexc1.Response).StatusCode;
}
}
finally
{
if (response!= null)
response.Close();
}
return result;
}
回答by SLaks
Your question is unclear.
你的问题不清楚。
If you're trying to read an HttpWebRequest's requeststream after other code has written to the stream (a POST request), it's not possible. (The stream is sent directly to the server and is not stored in memory)
如果您在其他代码写入流(POST 请求)后尝试读取HttpWebRequest的请求流,则这是不可能的。(流直接发送到服务器,不存储在内存中)
Instead, you'll need to expose your own MemoryStream, then write it to the HttpWebRequest's request stream wen you're ready to send the request.
相反,您需要公开您自己的 MemoryStream,然后将其写入 HttpWebRequest 的请求流,然后您就可以发送请求了。
回答by Bill Paetzke
Try using Fiddler. Worked for me.
尝试使用Fiddler。对我来说有效。
Fiddler:
小提琴手:
- is a Web Debugging Proxy
- logs all HTTP(S) traffic between your computer and the Internet
- allows you to inspect all HTTP(S) traffic, set breakpoints, and "fiddle" with data
- is freeware
- can debug traffic from all almost all apps (web browsers and more)
- 是一个网页调试代理
- 记录您的计算机和 Internet 之间的所有 HTTP(S) 流量
- 允许您检查所有 HTTP(S) 流量、设置断点和“摆弄”数据
- 是免费软件
- 可以调试几乎所有应用程序(网络浏览器等)的流量

