Big files uploading (WebException: The connection was closed unexpectedly)

后端 未结 4 750
广开言路
广开言路 2021-02-06 08:12

UPDATED

See post #3 below.

There is a need to upload a file to the web automatically (without browser). Host - Mini File Host v1.2 (if this does

4条回答
  •  不知归路
    2021-02-06 08:21

    Update : nope, there is no redirect.

    screenshot

    Read RFC2388 few times, rewrote the code and it finally worked (i guess the trouble was in utf-read trailing boundary instead of correct 7 bit ascii). Hooray? Nope :(. Only small files are transfered, big ones throwing "The connection was closed unexpectedly".

    System.Net.WebException was unhandled by user code
      Message="The underlying connection was closed: The connection was closed unexpectedly."
      Source="Uploader"
      StackTrace:
       at Uploader.Upload.ProcessUpload(String FilePath, String description, String password) in F:\MyDocuments\Visual Studio 2008\Projects\Uploader\Uploader.cs:line 96
       at Uploader.Form1.backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in F:\MyDocuments\Visual Studio 2008\Projects\Uploader\Form1.cs:line 45
       at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument) 
    

    I know that's a bug with .net stack and few solutions exists :

    1) increase both Timeout and ReadWriteTimeout of request

    2) assign request.KeepAlive = false and System.Net.ServicePointManager.Expect100Continue = false

    3) set ProtocolVersion to 1.0 But neither one of them nor all of them altogether help in my case. Any ideas?

    EDIT - Source code:

    // .. request created, required params applied
    httpWebRequest.ProtocolVersion = HttpVersion.Version10; // fix 1
    httpWebRequest.KeepAlive = false; // fix 2
    httpWebRequest.Timeout = 1000000000; // fix 3
    httpWebRequest.ReadWriteTimeout = 1000000000; // fix 4
    // .. request processed, data written to request stream
    string strResponse = "";            
    try
    {
        using (WebResponse httpResponse = httpWebRequest.GetResponse()) // error here
            {
                using (Stream responseStream = httpResponse.GetResponseStream())
                {
                    using (StreamReader streamReader = new StreamReader(responseStream))
                        {
                            strResponse = streamReader.ReadToEnd();
                        }
                    }
                }
            }
    catch (WebException exception)
    {
        throw exception;
    }
    

提交回复
热议问题