C# - HttpWebRequest - POST

11,725

Solution 1

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?

Solution 2

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):

HttpWebRequest httpWebRequest = HttpWebRequest.Create("http://test") as HttpWebRequest;
httpWebRequest.SendChunked = true;
MessageBox.Show("|" + httpWebRequest.TransferEncoding + "|");

From System.Net.HttpWebRequest.SerializeHeaders():

if (this.HttpWriteMode == HttpWriteMode.Chunked)
{
    this._HttpRequestHeaders.AddInternal("Transfer-Encoding", "chunked");
}
else if (this.ContentLength >= 0L)
{
    this._HttpRequestHeaders.ChangeInternal("Content-Length", this._ContentLength.ToString(NumberFormatInfo.InvariantInfo));
}
Share:
11,725

Related videos on Youtube

jonathanpeppers
Author by

jonathanpeppers

Xamarin MVP and lead developer on the popular apps Hanx Writer and Draw a Stickman. I work at Microsoft on the Xamarin.Android team.

Updated on April 16, 2022

Comments

  • jonathanpeppers
    jonathanpeppers about 2 years

    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.

  • jonathanpeppers
    jonathanpeppers over 14 years
    My XmlWriter is in a using block, and it merely writes some elements and attributes with some looping. It calls WriteEndDocument to end the document. I am beginning to wonder if it's the server and not .Net, but I do not have a windows server to test with, although the version of Apache is pretty new.
  • Aaronaught
    Aaronaught over 14 years
    WriteEndDocument does not actually close the stream. Somewhere in your code, you are invoking GetRequestStream (or BeginGetRequestStream); once you are finished writing your XML, you need to invoke the Close method on that Stream instance. If you don't, it won't know when you're finished uploading and the connection will sit open forever.
  • Cory Charlton
    Cory Charlton over 14 years
    @Aaron: My edited post has an example to show this is not the case, at least not in the TransferEncoding property. Setting SendChunked = true still has an empty TransferEncoding value. Whether this is true when the actual request is sent I cannot say.
  • Aaronaught
    Aaronaught over 14 years
    You are using the XmlWriter, which will dispose the XmlWriter and only the XmlWriter. It is the Stream that you need to call Dispose on (or Close, which is the recommended method). XmlWriter will not do this for you.