尝试更新OData服务时,为什么HttpWebRequest.GetResponse()挂起? [英] Why does HttpWebRequest.GetResponse() hang when trying to update an OData service?

查看:83
本文介绍了尝试更新OData服务时,为什么HttpWebRequest.GetResponse()挂起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#编写OData使用者.我可以成功删除条目,但是由于某些原因,当我尝试更新时,它只是挂在/冻结在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())
{
}

推荐答案

摆弄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.

XDocument doc = XDocument.Parse(
@"<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?>
<entry 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"">
  <category scheme=""http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"" term=""MetastormModel.SimpleTest"" />
  <title />
  <author>
    <name />
  </author>
  <updated>2011-07-19T09:28:03.173654Z</updated>
  <id>http://localhost/odata/service.svc/SimpleTests(7)</id>
  <content type=""application/xml"">
    <m:properties>
      <d:ID m:type=""Edm.Int32"">7</d:ID>
      <d:datetimecol m:type=""Edm.DateTime"">1950-01-01T00:00:00</d:datetimecol>
      <d:imagecol m:type=""Edm.Binary"" m:null=""true"" />
      <d:intcol m:type=""Edm.Int32"">-44</d:intcol>
      <d:moneycol m:type=""Edm.Decimal"">12345.0000</d:moneycol>
      <d:realcol m:type=""Edm.Single"">22.11</d:realcol>
      <d:textcol>Hello There</d:textcol>
      <d:varcharcol>EFGH</d:varcharcol>
    </m:properties>
  </content>
</entry>");

HttpWebRequest request =
 (HttpWebRequest)HttpWebRequest.Create("http://localhost/odata/service.svc/SimpleTests(7)");
request.Method = "MERGE";
request.Accept = "application/atom+xml,application/xml";
request.Timeout = 20000;
request.ContentType = "application/atom+xml";
using (MemoryStream stream = new MemoryStream())
{
    using (var writer = XmlWriter.Create(stream))
    {
        doc.WriteTo(writer);
        writer.Close();
    }
    request.ContentLength = stream.Length;
    using (var writer = XmlWriter.Create(request.GetRequestStream()))
    {
        doc.WriteTo(writer);
        writer.Close();
    }
}

这篇关于尝试更新OData服务时,为什么HttpWebRequest.GetResponse()挂起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆