为什么 HttpWebRequest.GetResponse() 在尝试更新 OData 服务时会挂起?
我正在用 C# 编写一个 OData 消费者。我可以成功删除一个条目,但由于某种原因,当我尝试更新时,它只是挂起/冻结在 GetResponse() 上。它甚至忽略了我的超时。下面是我的测试代码。有什么明智的建议吗?
XDocument doc = XDocument.Parse(
@"<?xml version=""1.0"" encoding=""iso-8859-1"" standalone=""yes""?>
<entry xml:base=""http://localhost/odata/service.svc/"" xmlns:d=""http://schemas.microsoft.com/ado/2007/08/dataservices"" xmlns:m=""http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"" xmlns=""http://www.w3.org/2005/Atom"">
<id>http://localhost/odata/service.svc/SimpleTests(7)</id>
<title type=""text""></title>
<updated>2011-07-18T12:19:43Z</updated>
<author>
<name />
</author>
<link rel=""edit"" title=""SimpleTest"" href=""SimpleTests(7)"" />
<category term=""MetastormModel.SimpleTest"" scheme=""http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"" />
<content type=""application/xml"">
<m:properties>
<d:ID m:type=""Edm.Int32"">7</d:ID>
<d:varcharcol>EFGH</d:varcharcol>
<d:intcol m:type=""Edm.Int32"">-44</d:intcol>
<d:realcol m:type=""Edm.Single"">22.11</d:realcol>
<d:datetimecol m:type=""Edm.DateTime"">1999-12-31T00:00:00</d:datetimecol>
<d:imagecol m:type=""Edm.Binary"" m:null=""true"" />
<d:moneycol m:type=""Edm.Decimal"">1000000.0000</d:moneycol>
<d:textcol>WOW!</d:textcol>
</m:properties>
</content>
</entry>");
HttpWebRequest request =
(HttpWebRequest)HttpWebRequest.Create("http://localhost/odata/service.svc/SimpleTests(7)");
request.Method = "PUT";
request.Accept = "application/atom+xml";
request.Timeout = 1000;
request.ContentType = "application/atom+xml;type=entry";
using (var writer = XmlWriter.Create(request.GetRequestStream()))
{
doc.WriteTo(writer);
writer.Close();
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
}
I'm writing an OData consumer in c#. I can successfully delete an entry, but for some reason it simply hangs / freezes on GetResponse() when I try to update. It even ignores my timeout. Below is my test code. Any intelligent suggestions?
XDocument doc = XDocument.Parse(
@"<?xml version=""1.0"" encoding=""iso-8859-1"" standalone=""yes""?>
<entry xml:base=""http://localhost/odata/service.svc/"" xmlns:d=""http://schemas.microsoft.com/ado/2007/08/dataservices"" xmlns:m=""http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"" xmlns=""http://www.w3.org/2005/Atom"">
<id>http://localhost/odata/service.svc/SimpleTests(7)</id>
<title type=""text""></title>
<updated>2011-07-18T12:19:43Z</updated>
<author>
<name />
</author>
<link rel=""edit"" title=""SimpleTest"" href=""SimpleTests(7)"" />
<category term=""MetastormModel.SimpleTest"" scheme=""http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"" />
<content type=""application/xml"">
<m:properties>
<d:ID m:type=""Edm.Int32"">7</d:ID>
<d:varcharcol>EFGH</d:varcharcol>
<d:intcol m:type=""Edm.Int32"">-44</d:intcol>
<d:realcol m:type=""Edm.Single"">22.11</d:realcol>
<d:datetimecol m:type=""Edm.DateTime"">1999-12-31T00:00:00</d:datetimecol>
<d:imagecol m:type=""Edm.Binary"" m:null=""true"" />
<d:moneycol m:type=""Edm.Decimal"">1000000.0000</d:moneycol>
<d:textcol>WOW!</d:textcol>
</m:properties>
</content>
</entry>");
HttpWebRequest request =
(HttpWebRequest)HttpWebRequest.Create("http://localhost/odata/service.svc/SimpleTests(7)");
request.Method = "PUT";
request.Accept = "application/atom+xml";
request.Timeout = 1000;
request.ContentType = "application/atom+xml;type=entry";
using (var writer = XmlWriter.Create(request.GetRequestStream()))
{
doc.WriteTo(writer);
writer.Close();
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在摆弄 Fiddler 之后,我想出了以下可行的解决方案。似乎解决问题的部分是指定 request.ContentLength。我不知道有更简单的方法来获取长度,所以如果您知道的话,请随时发表评论。
After fiddling around with Fiddler, I came up with the following solution, which works. The part that seems to solve the problem is specifying request.ContentLength. I don't know of a simpler way to get the length, so feel free to comment if you do.
PUT 请求不会返回除 HTTP 代码(204 - 无内容)之外的任何内容。
有关详细信息,请参阅OData 规范的这一部分。
A PUT request does not return any content other than a HTTP code (204 - No Content) for success.
See this part of the OData spec for details.