locked
The underlying connection was closed: An unexpected error occurred on Stream.Write RRS feed

  • Question

  • User-1728222991 posted

    This is code for upload file via httprequest

     static Stream formDataStream = null;
            private void button3_Click(object sender, EventArgs e)
            {
                 DialogResult result = this.openFileDialog1.ShowDialog();
                 if (result == DialogResult.OK)
                 {
                    string  fileUrl = openFileDialog1.FileName;
    
                     HttpWebRequest myreq =
                      (HttpWebRequest)WebRequest.Create("http://www.ssss.com/video.xml");
                     string boundaryString = "----WebKitFormBoundaryrqinj7B0Pj91NMg9";
    
                     // Set the http request header \\
                     myreq.Method = WebRequestMethods.Http.Post;
                     myreq.ContentType = "multipart/form-data; boundary=" + boundaryString;
                     myreq.KeepAlive = true;
                     myreq.Credentials = new NetworkCredential("username", "passwd");
                    
                   formDataStream = getFormDataStream(boundaryString, fileUrl); //get the stream to send via http POST
                   myreq.ContentLength = formDataStream.Length;
                     // start the asynchronous operation
               
                     myreq.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myreq);
                   
                 }
            }
    
            private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
            {
                HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
            
                // End the operation
                Stream reqStream = request.EndGetRequestStream(asynchronousResult);
    
    
                formDataStream.Position = 0;           
    
                byte[] buffer = new byte[1024];
                for (int len; (len = formDataStream.Read(buffer, 0, buffer.Length)) > 0; )
                {
                    reqStream.Write(buffer, 0, len);               
                }
                // Start the asynchronous operation to get the response
                request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
            }
            private static void GetResponseCallback(IAsyncResult asynchronousResult)
            {
                HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
    
                // End the operation
                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
                Stream streamResponse = response.GetResponseStream();
              
            }
       
    

    But i am getting the error 

    Message=The underlying connection was closed: An unexpected error occurred on a receive.   Message=An established connection was aborted by the software

    in your host machine

    on this line

                byte[] buffer = new byte[1024];

       
             for (int len; (len = formDataStream.Read(buffer, 0, buffer.Length)) > 0; )
                {
                    reqStream.Write(buffer, 0, len);               
                }

    I tried the same code with setting KeepAlive as False, but no change in response. What i want is need to upload a large file as small chunks into server via http post from a desktop application

     

    FormDataStream contains the whole data to send and while i am trying to write the content to httprequest i am getting this error,

    I follow this pattern after reading comment on the forum "http://www.wintellect.com/CS/blogs/jeffreyr/archive/2009/02/08/httpwebrequest-its-request-stream-and-sending-data-in-chunks.aspx"

     

    Wednesday, July 27, 2011 8:20 AM

Answers

All replies