Why is writing XML in my POST HttpWebRequest causing my request to hang in .net 3.5 but not .net 4?

StackOverflow https://stackoverflow.com/questions/17536966

  •  02-06-2022
  •  | 
  •  

Question

So take the following code (very simplified test code of our actual application):

    public string Test()
    {
        string content = @"<entry xmlns:yt=""http://gdata.youtube.com/schemas/2007"" xmlns:media=""http://search.yahoo.com/mrss/"" xmlns=""http://www.w3.org/2005/Atom""><title>Matt Test Event</title><summary>Testing 1234</summary><yt:when start=""2013-07-08T21:56:49.434569Z"" /></entry>";

        string requestUrl =
            "https://gdata.youtube.com/feeds/api/users/xossports/live/events/fv7cuJbtZrGzYFNNa-WfWubz3qleq8ai?inline=true";

        var request = (HttpWebRequest)WebRequest.Create(requestUrl);
        request.Accept = "application/atom+xml";
        request.ContentType = "application/atom+xml";
        request.Headers.Add("GData-Version", "2");
        request.Headers.Add("X-GData-Key", string.Concat("key=", DEVELOPER_KEY));
        request.Method = HttpMethods.Post;

        //using (var writer = new StreamWriter(request.GetRequestStream()))
        //    writer.Write(content);

        var requestBody = XDocument.Parse(content);
        using (var writer = XmlWriter.Create(request.GetRequestStream()))
            requestBody.Save(writer);

        using (var response = (HttpWebResponse)request.GetResponse())
        using (var reader = new StreamReader(response.GetResponseStream()))
            return reader.ReadToEnd();
    }

    static void Main(string[] args)
    {
        using (var service = new YouTubeService())
            Console.WriteLine(service.Test());
    }

When I run this in a console application that's targeting the .Net 4 framework, everything works fine (I correctly get a 401 bad request response, since some things aren't setup correctly in this).

However, if I run this while my application is targeting the .Net 3.5 framework, it freezes on the request.GetResponse() call. No error, never times out, just indefinitely hangs.

The commented out StreamWriter() call shows that it works perfectly fine when I write a plain string to the stream instead of using the XmlWriter, so while I have a perfectly fine work around (after hours of hair pulling) does anyone have any idea why XmlWriter causes the response call to completely hang only in .net 3.5?

Was it helpful?

Solution

In 4.0 when you call HttpWebRequest.GetResponse(), it will "close" the request stream if it's not been done before, thus the request continues. On 3.5, it will remain opened and wait for it to be closed before the request is sent.

The easiest "fix" (or workaround) is to explicitly close the request stream after writing to it:

var requestBody = XDocument.Parse(content);
using (var writer = XmlWriter.Create(request.GetRequestStream()))
{
    requestBody.Save(writer);
}
request.GetRequestStream().Close();

OTHER TIPS

I have no idea why there would be a difference between 3.5 and 4.0. XmlWriter doesn't seem to have changed that much or at all. However, I have encountered some errors when not setting some XmlWriterSettings properties when creating an XmlWriter, I would try:

using (var writer = XmlWriter.Create(request.GetRequestStream(), new XmlWriterSettings{

 CloseOutput = true,
 Encoding = Encoding.UTF-8 // not sure of this syntax. check it out.

}))
....
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top