Error using HttpWebRequest to upload files using PUT

We have a .NET 2.0 WinForms application that should upload files to the IIS6 server via WebDav. From time to time, we receive complaints from the remote office that they receive one of the following error messages

  • The connected connection was closed: an unexpected error occurred while sending.
  • The connected connection was closed: An unexpected error occurred while receiving.

This only happens with large files (~ 20 MB plus). I tested it with a 40 MB file from my home computer and tried to put "Sleep in a loop to simulate a slow connection, so I suspect this is due to network problems at the end ... but

  • IT in a remote office does not help
  • I would like to rule out the possibility that my code is to blame.

So - someone can identify any errors or suggest some workarounds that can "bulletproof" the code against this problem. Thanks for any help. The following version of the code is subtracted:

    public bool UploadFile(string localFile, string uploadUrl)
    {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uploadUrl);

        try
        {
            req.Method = "PUT";
            req.AllowWriteStreamBuffering = true;
            req.UseDefaultCredentials = Program.WebService.UseDefaultCredentials;
            req.Credentials = Program.WebService.Credentials;
            req.SendChunked = false;
            req.KeepAlive = true;
            Stream reqStream = req.GetRequestStream();
            FileStream rdr = new FileStream(localFile, FileMode.Open, FileAccess.Read);
            byte[] inData = new byte[4096];
            int bytesRead = rdr.Read(inData, 0, inData.Length);

            while (bytesRead > 0)
            {
                reqStream.Write(inData, 0, bytesRead);
                bytesRead = rdr.Read(inData, 0, inData.Length);
            }

            reqStream.Close();
            rdr.Close();

            System.Net.HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            if (response.StatusCode != HttpStatusCode.OK && response.StatusCode!=HttpStatusCode.Created)
            {
                MessageBox.Show("Couldn't upload file");
                return false;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
            return false;
        }
        return true;
    }
+3
source share
6 answers

Try setting KeepAlive to false:

req.KeepAlive = false;

This will allow you to close the connection again and again. This will not allow the use of a persistent connection . I found many links on the Internet that suggested this to solve a similar error of yours. This is a relevant link..

, HTTP PUT ( HTTP POST) . FTP /. , , . , . HTTP, . , , - -, - , , - .

+6

, WebClient:

using (WebClient client = new WebClient())
{
    client.UseDefaultCredentials = Program.WebService.UseDefaultCredentials;
    client.Credentials = Program.WebService.Credentials;
    client.UploadFile(uploadUrl, "PUT", localFile);
}
+5

, POST, , , .

req.ContentType = "application/octet-stream";
req.ContentLength = inData.Length;

: HTTPWebrequest (multipart/form-data)

, , , ContentType - , , , , multipart

+1

, , [ Http Keep-Alives] [-] IIS.

+1

Start by checking the basic configuration. The default values ​​for any of the following options can cause problems downloading the file — including terminating the connection. I believe that IIS 6 will never allow file uploads> 2 GB (even if it can complete, regardless of configuration). Msdn describes it beautifully .

<httpRuntime executionTimeout = "30"  maxRequestLength="200"/>

EDIT: this, of course, is an ASP.NET configuration that assumes you are using your own webdav server or

0
source

Source: https://habr.com/ru/post/1706183/


All Articles