Reuest.InputStream.Length 为 0,RequestType 为 Get
我正在尝试使用 httpwebrequest 将一个简单的 XML 字符串 POST 到 ASP.NET 中另一个 Web 服务器的网页。
这是示例代码:
public static bool Send()
{
string xml = "<Root><SEQNO>7</SEQNO></Root>";
Byte[] bytes = System.Text.Encoding.UTF8.GetBytes(xml);
HttpWebRequest objWebRequest = (HttpWebRequest)WebRequest.Create("Url");
objWebRequest.ContentType = "text/xml";
objWebRequest.Method = WebRequestMethods.Http.Post;
objWebRequest.Timeout = 10000;
objWebRequest.ContentLength = bytes.Length;
Stream objRequestStream = null;
objRequestStream = objWebRequest.GetRequestStream();
objRequestStream.Write(bytes, 0, bytes.Length);
objRequestStream.Close();
HttpWebResponse objWebResponse = (HttpWebResponse)objWebRequest.GetResponse();
if (objWebResponse.StatusCode == HttpStatusCode.OK)
{
objWebResponse.Close();
return true;
}
else
{
return false;
}
}
此代码运行正常,但在它发布到的页面上我检查 Request.RequestType
始终给出“Get”和“Request”。 InputStream
不为空,但其长度始终为 0。我无法接收该字符串。
这是怎么回事?我错过了什么吗?请建议我如何解决这个问题。
I am trying to use httpwebrequest to POST a simple XML string to another webserver's web page in ASP.NET.
Here is the sample code:
public static bool Send()
{
string xml = "<Root><SEQNO>7</SEQNO></Root>";
Byte[] bytes = System.Text.Encoding.UTF8.GetBytes(xml);
HttpWebRequest objWebRequest = (HttpWebRequest)WebRequest.Create("Url");
objWebRequest.ContentType = "text/xml";
objWebRequest.Method = WebRequestMethods.Http.Post;
objWebRequest.Timeout = 10000;
objWebRequest.ContentLength = bytes.Length;
Stream objRequestStream = null;
objRequestStream = objWebRequest.GetRequestStream();
objRequestStream.Write(bytes, 0, bytes.Length);
objRequestStream.Close();
HttpWebResponse objWebResponse = (HttpWebResponse)objWebRequest.GetResponse();
if (objWebResponse.StatusCode == HttpStatusCode.OK)
{
objWebResponse.Close();
return true;
}
else
{
return false;
}
}
This code runs OK, but on the page where it post to I checkRequest.RequestType
which always gives 'Get'and Request.InputStream
is not null but its length is always 0. I am not able to receive the string.
What's wrong here? Am I missing something? Please suggest how I can fix this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很奇怪,但我自己找到了解决方案,它在 web.config 中,
当我使用 cookieless="AutoDetect" 或 cookieless="True" 时,接收器上的请求类型始终为 Get,Request.InputStream.Length 为 0。当我将其设置为 false 时,一切都很好。
到目前为止它有效,但我想稍后再深入研究。
This is strange but I found the solution myself, its in web.config,
When I use cookieless="AutoDetect" or cookieless="True", I had Request type on receiver always as Get and Request.InputStream.Length was 0. When I set to it to false, everything is fine.
So far it worked but I would like to dig this more later.