开发者

HttpWebRequest stream write never completes

开发者 https://www.devze.com 2023-03-09 22:24 出处:网络
I\'m posting a file with HttpWebRequest开发者_运维知识库, along with a header and footer. The header (ca. 0.5K) and the actual file seem to write fine, but with large files (ca. 15MB), the footer (whi

I'm posting a file with HttpWebRequest开发者_运维知识库, along with a header and footer. The header (ca. 0.5K) and the actual file seem to write fine, but with large files (ca. 15MB), the footer (which is like 29 bytes) never seems to write.

using (Stream requestStream = request.GetRequestStream()) {
    requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

    byte[] buffer = new byte[Math.Min(4096L, fileSize)];
    int bytesRead = 0;
    while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) {
        requestStream.Write(buffer, 0, bytesRead);
    }

    // next line never completes                        
    requestStream.Write(postFooterBytes, 0, postFooterBytes.Length);

    // code below is never reached
    Console.WriteLine("Why do I never see this message in the console?");
}

Any thoughts?

ETA: Tried flushing the stream before the last Write(), on the off chance it would help, but to no effect.

Edited again: Added using() to clarify that I'm not a complete idiot. Note also BTW that this is inside another using() block for fileStream.


Solved: Turned off AllowWriteStreamBuffering on the HttpWebRequest. Looks like when it's on, whatever Write() call writes the last byte, it doesn't return till the internal buffer's cleared. So the last Write() was eventually competing, just not till I ran out of patience.

And since what I was originally trying to do was determine progress, turning off buffering makes things clearer anyway.


A common problem is forgetting closing the request stream. One of the symptoms you'll see is that the request is never made. It's quite likely that the write really is completing, but since you didn't close the request stream, the call to HttpWebRequest.GetResponse() appears not to be executed.

Try the following and see if it makes a difference:

using (var requestStream = myRequest.GetRequestStream())
{
    // write to the request stream here
}
// Now try to get the response.

Another possible issue is the size of the data. First, are you sure that the server can handle a 15 MB upload? Secondly, if you're doing this on a slow connection, 15 MB can take a while to send. I have what's considered a "fast" upstream connection at 1.5 megabits/sec. That's, at best, 0.15 megabytes per second. Sending 15 megabytes will take over a minute and a half.

One other possibility is that the request is timing out. You want to look into the HttpWebRequest.Timeout and ReadWriteTimeout properties.


When you are building your request, your content length should include the headers as well, make sure its not just set to the file length. The other thing you may try is to call .Flush() on the stream when all is said and done. I'm not sure of the implication of closing the stream for the HttpClient as Jim suggests, it may work, it may make it worse.

Does using System.Net.WebClient not offer enough flexibility for you? Theres a nice UploadFile() method you can use.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号