C# Http Post Example

Last post 10-04-2007 4:42 PM by BarryR. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 10-04-2007 12:17 AM

    • BarryR
    • Top 10 Contributor
    • Joined on 07-20-2007
    • San Diego
    • Posts 777

    C# Http Post Example

    I wanted to follow up with a few questions and some requests with a simple upload method to show how to do a HTTP Post.  This should be a simple example that shows how to do this while not using much memory and being fairly efficient.

    This example assumes you have Set up a web reference to do the login and IMFS GetUploadNode calls just for simplicity as Authentication and IMFS.

     public void WebRequestUploadSuccessTest()
            {
                // Setup user params
                string VideoFile = @"f:\vids\testvideo.avi";

                // Setup soap classes
                Authentication auth = new Authentication();
                auth.Url = "https://services.nirvanix.com/ws/Authentication.asmx";
                IMFS imfs = new IMFS();
                imfs.Url = "http://services.nirvanix.com/ws/IMFS.asmx";

                // Login and get sessionToken
                string sessionToken = auth.Login(appKey, Username, Password);
                // Get the upload node
                UploadNode node = imfs.GetUploadNode(sessionToken, new FileInfo(VideoFile).Length);
                // Format the URL with
                string url = "http://" + node.IPAddress + "/Upload.ashx?uploadToken=" + node.AccessToken + "&destFolderPath=/httpupload/";

                string response = UploadFilesToRemoteUrl(url, VideoFile);

                Console.Write(response);
            }

            /// <summary>
            /// A method to upload files to the remote server streaming.
            /// </summary>
            private string UploadFilesToRemoteUrl(string url, string file)
            {
                // Create a boundry
                string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");

                // Create the web request
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
                httpWebRequest.Method = "POST";
                httpWebRequest.KeepAlive = true;

                httpWebRequest.Credentials =
                System.Net.CredentialCache.DefaultCredentials;

                // Get the boundry in bytes
                byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

                // Get the header for the file upload
                string headerTemplate = "Content-Disposition: form-data; name=\"{0}\";filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";

                // Add the filename to the header
                string header = string.Format(headerTemplate, "file", file);

                //convert the header to a byte array
                byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);

                // Add all of the content up.
                httpWebRequest.ContentLength = new FileInfo(file).Length + headerbytes.Length + (boundarybytes.Length * 2) + 2;

                // Get the output stream
                Stream requestStream = httpWebRequest.GetRequestStream();

                // Write out the starting boundry
                requestStream.Write(boundarybytes, 0, boundarybytes.Length);

                // Write the header including the filename.
                requestStream.Write(headerbytes, 0, headerbytes.Length);

                // Open up a filestream.
                FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
                
                // Use 4096 for the buffer
                byte[] buffer = new byte[4096];

                int bytesRead = 0;
                // Loop through whole file uploading parts in a stream.
                while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    requestStream.Write(buffer, 0, bytesRead);
                    requestStream.Flush();
                }

                boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");

                // Write out the trailing boundry
                requestStream.Write(boundarybytes, 0, boundarybytes.Length);

                // Close the request and file stream
                requestStream.Close();
                fileStream.Close();

                WebResponse webResponse = httpWebRequest.GetResponse();

                Stream responseStream = webResponse.GetResponseStream();
                StreamReader responseReader = new StreamReader(responseStream);

                string responseString = responseReader.ReadToEnd();

                // Close response object.
                webResponse.Close();

                return responseString;
            }

     

     

    Let me know if there are any problems with this example.

    BR
     

    IM Support (Feel free to add me)

    MSN: barryruffner@live.com
    Gmail: barryruffner@gmail.com
    Filed under: , , ,
  • 10-04-2007 3:45 PM In reply to

    Re: C# Http Post Example

    Thanks for the example.

    I'll test it to see if its much faster than the SOAP methods.  Should l test with different sized buffers?  Or keep it at 4K.

     Cheers

    Richard

  • 10-04-2007 4:42 PM In reply to

    • BarryR
    • Top 10 Contributor
    • Joined on 07-20-2007
    • San Diego
    • Posts 777

    Re: C# Http Post Example

    I didn't spend too much time on this example optimizing for performance(Mostly just making sure it's readable and easy to understand) other than making sure it was close to the maximum, realistically you should always send the largest amount thats possible given your memory constraints.  As I understand the web request object it will use TCP/IP and the MTU (Maximum Transmission Unit?) size to determine the packet sizes on the outbound socket so as long as you are sending pieces that are that big or more then it should optimize itself.  Depending on the application 8K to 64k has worked well for my buffer sizes but each implementation can be slightly different. 

    If you want to have the stream handle its own buffer sizes you can get rid of the responseStream.flush() call so it will build up an internal buffer before sending whatever it thinks is appropriate since it will flush when its "Full".  I'm not sure what that amount is though a bit of reading up on the C# http stream buffer would be in order I think.

    Please let us know the results of your testing its always good to have more than one eye on performance and optimization of code.

    Regards,
        Barry R.
     

    IM Support (Feel free to add me)

    MSN: barryruffner@live.com
    Gmail: barryruffner@gmail.com
Page 1 of 1 (3 items)