C# - HttpWebRequest - POST
我正在尝试向 Apache Web 服务器发送 Http POST。
我发现似乎需要设置 ContentLength 才能使请求正常工作。
我宁愿直接从 GetRequestStream() 创建一个 XmlWriter 并将 SendChunked 设置为 true,但这样做时请求会无限期挂起。
以下是我的请求的创建方式:
private HttpWebRequest MakeRequest(string url, string method)
{
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
request.Method = method;
request.Timeout = Timeout; //Property in my class, assume it's 10000
request.ContentType = "text/xml"; //I am only writing xml with XmlWriter
if (method != WebRequestMethods.Http.Get)
{
request.SendChunked = true;
}
return request;
}
How can I make SendChunked work so I don't have set ContentLength?我不认为有理由在将 XmlWriter 的字符串发送到服务器之前将其存储在某处。
编辑:这是导致问题的我的代码:
using (Stream stream = webRequest.GetRequestStream())
{
using (XmlWriter writer = XmlWriter.Create(stream, XmlTags.Settings))
{
Generator.WriteXml<TRequest>(request, writer);
}
}
在我没有使用从 GetRequestStream() 返回的 Stream 对象之前,我假设 XmlWriter 在处理时关闭了流,但事实并非如此。
下面的答案之一,让我来回答一下。我会将它们标记为答案。
就 HttpWebRequest 而言,我的原始代码工作得很好。
I am trying to make an Http POST to an Apache web server.
I am finding that setting ContentLength seems to be required for the request to work.
I would rather create an XmlWriter directly from GetRequestStream() and set SendChunked to true, but the request hangs indefinitely when doing so.
Here is how my request is created:
private HttpWebRequest MakeRequest(string url, string method)
{
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
request.Method = method;
request.Timeout = Timeout; //Property in my class, assume it's 10000
request.ContentType = "text/xml"; //I am only writing xml with XmlWriter
if (method != WebRequestMethods.Http.Get)
{
request.SendChunked = true;
}
return request;
}
How can I make SendChunked work so I do not have to set ContentLength? I do not see a reason to store the XmlWriter's string somewhere before sending it to the server.
EDIT: Here is my code causing the problem:
using (Stream stream = webRequest.GetRequestStream())
{
using (XmlWriter writer = XmlWriter.Create(stream, XmlTags.Settings))
{
Generator.WriteXml<TRequest>(request, writer);
}
}
Before I did not have a using on the Stream object returned from GetRequestStream(), I assumed XmlWriter closed the stream when disposed, but this is not the case.
One of the answers below, let me to this. I'll mark them as the answer.
As far as HttpWebRequest is concerned, my original code works just fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该按照你写的方式工作。我们能看到实际执行上传的代码吗?您记得关闭流吗?
This should work the way you have it written. Can we see the code that actually does the uploading? Are you remembering to close the stream?
查看示例 http://msdn.microsoft .com/en-us/library/system.net.httpwebrequest.sendchunked.aspx 他们仍然设置内容长度。实际上,最重要的是,如果您要发送数据,您需要告诉接收者您将发送多少数据。为什么在发送请求之前不知道要发送多少数据?
内容长度:
属性值
类型:系统..::.Int64
发送到 Internet 资源的数据字节数。默认值为 -1,表示该属性尚未设置,并且没有要发送的请求数据。
为 Aaron 编辑(我错了):
来自 System.Net.HttpWebRequest.SerializeHeaders():
Looking at the example at http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.sendchunked.aspx they still set a content length. Really the bottom line is that if you are sending data you need to tell the receiver how much data you will be sending. Why don't you know how much data you are sending before you send the request?
ContentLength:
Property Value
Type: System..::.Int64
The number of bytes of data to send to the Internet resource. The default is -1, which indicates the property has not been set and that there is no request data to send.
Edit for Aaron (I was wrong):
From System.Net.HttpWebRequest.SerializeHeaders():
我更喜欢使用通用方法来遵守此类内容。查看下面的 XML 发送方请求。它将序列化您的 XML,然后使用适当的 ContentType 发送它:
I prefer to use a generic method to comply this kind of stuff. Take a look at the XML sender request below. It will serialize your XML and then send it with the appropriate ContentType :