예제 #1
0
        private string HttpUploadFile(string url, NameValueCollection nvc, Stream stream, string file)
        {
            var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
            var boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

            var wr = (HttpWebRequest)WebRequest.Create(url);
            wr.ContentType = "multipart/form-data; boundary=" + boundary;
            wr.Method = "POST";
            wr.KeepAlive = true;
            wr.Credentials = CredentialCache.DefaultCredentials;

            wr.Headers.Add("Accept-Language", "en-gb,en;q=0.5");
            wr.Headers.Add("Accept-Encoding", "gzip,deflate");
            wr.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
            wr.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            wr.AllowWriteStreamBuffering = false;
            wr.SendChunked = false;
            //wr.Timeout = 1000000000;

            const string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
            var header = string.Format(headerTemplate, "file", file, "application/octet-stream");
            var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);

            const string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";

            var nvcByteCount = 0;
            foreach (string key in nvc.Keys)
            {
                var formitem = string.Format(formdataTemplate, key, nvc[key]);
                var formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                nvcByteCount += formitembytes.Length;
            }

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

            wr.ContentLength = stream.Length + headerbytes.Length + ((nvc.Count + 1) * boundarybytes.Length) + nvcByteCount + trailer.Length;

            var rs = wr.GetRequestStream();


            foreach (string key in nvc.Keys)
            {
                rs.Write(boundarybytes, 0, boundarybytes.Length);
                var formitem = string.Format(formdataTemplate, key, nvc[key]);
                var formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                rs.Write(formitembytes, 0, formitembytes.Length);
            }
            rs.Write(boundarybytes, 0, boundarybytes.Length);
            rs.Write(headerbytes, 0, headerbytes.Length);

            var buffer = new byte[bufferSize];
            int bytesRead;
            var bytesUploaded = 0;
            while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
            {
                rs.Write(buffer, 0, bytesRead);

                //raise event with upload progress
                bytesUploaded += bytesRead;
                var eventArgs = new UploadProgressEventArgs
                {
                    bytesUploaded = bytesUploaded,
                    bytesTotal = stream.Length
                };

                ProgressEvent(this, eventArgs);

            }

            rs.Write(trailer, 0, trailer.Length);
            rs.Close();

            HttpWebResponse wresp = null;
            var rawResponse = "";

            try
            {
                wresp = (HttpWebResponse)wr.GetResponse();
                var stream2 = wresp.GetResponseStream();
                var reader2 = new StreamReader(stream2);
                rawResponse = reader2.ReadToEnd();

            }
            catch (Exception ex)
            {
                if (wresp != null)
                {
                    wresp.Close();
                }
            }

            var guid = "";
            if (rawResponse.Length > 0)
            {
                var doc = new XmlDocument();
                doc.LoadXml(rawResponse);
                string[] keyParts = doc.SelectSingleNode("//Key").InnerText.Split('/');
                guid = keyParts[keyParts.Length - 2];
            }
            return guid;
        }
예제 #2
0
        private string HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc)
        {
            var boundary      = "---------------------------" + DateTime.Now.Ticks.ToString("x");
            var boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

            var wr = (HttpWebRequest)WebRequest.Create(url);

            wr.ContentType = "multipart/form-data; boundary=" + boundary;
            wr.Method      = "POST";
            wr.KeepAlive   = true;
            wr.Credentials = CredentialCache.DefaultCredentials;

            //todo find out if necessary
            wr.Headers.Add("Accept-Language", "en-gb,en;q=0.5");
            wr.Headers.Add("Accept-Encoding", "gzip,deflate");
            wr.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
            wr.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
            wr.AllowWriteStreamBuffering         = false;
            wr.SendChunked = false;
            //wr.Timeout = 1000000000;
            //request.ContentType = contentType;

            const string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
            var          header         = string.Format(headerTemplate, paramName, file, contentType);
            var          headerbytes    = System.Text.Encoding.UTF8.GetBytes(header);

            var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);

            const string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";

            var nvcByteCount = 0;

            foreach (string key in nvc.Keys)
            {
                var formitem      = string.Format(formdataTemplate, key, nvc[key]);
                var formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                nvcByteCount += formitembytes.Length;
            }
            var trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");

            wr.ContentLength = fileStream.Length + headerbytes.Length + ((nvc.Count + 1) * boundarybytes.Length) + nvcByteCount + trailer.Length;


            var rs = wr.GetRequestStream();


            foreach (string key in nvc.Keys)
            {
                rs.Write(boundarybytes, 0, boundarybytes.Length);
                var formitem      = string.Format(formdataTemplate, key, nvc[key]);
                var formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                rs.Write(formitembytes, 0, formitembytes.Length);
            }
            rs.Write(boundarybytes, 0, boundarybytes.Length);

            rs.Write(headerbytes, 0, headerbytes.Length);

            var buffer = new byte[bufferSize];
            int bytesRead;
            var bytesUploaded = 0;

            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                rs.Write(buffer, 0, bytesRead);

                //raise event with upload progress
                bytesUploaded += bytesRead;
                var eventArgs = new UploadProgressEventArgs
                {
                    bytesUploaded = bytesUploaded,
                    bytesTotal    = fileStream.Length
                };

                ProgressEvent(this, eventArgs);
            }
            fileStream.Close();

            rs.Write(trailer, 0, trailer.Length);
            rs.Close();

            HttpWebResponse wresp    = null;
            var             response = "";

            try
            {
                wresp = (HttpWebResponse)wr.GetResponse();
                var stream2 = wresp.GetResponseStream();
                var reader2 = new StreamReader(stream2);
                response = reader2.ReadToEnd();
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
                if (wresp != null)
                {
                    wresp.Close();
                }
            }

            return(response);
        }
예제 #3
0
        private string HttpUploadFile ( string url, string file, string paramName, string contentType, NameValueCollection nvc )
        {
            var boundary = "---------------------------" + DateTime.Now.Ticks.ToString( "x" );
            var boundarybytes = System.Text.Encoding.ASCII.GetBytes( "\r\n--" + boundary + "\r\n" );

            var wr = (HttpWebRequest)WebRequest.Create( url );
            wr.ContentType = "multipart/form-data; boundary=" + boundary;
            wr.Method = "POST";
            wr.KeepAlive = true;
            wr.Credentials = CredentialCache.DefaultCredentials;

            //todo find out if necessary
            wr.Headers.Add( "Accept-Language", "en-gb,en;q=0.5" );
            wr.Headers.Add( "Accept-Encoding", "gzip,deflate" );
            wr.Headers.Add( "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" );
            wr.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
            wr.AllowWriteStreamBuffering = false;
            wr.SendChunked = false;
            //wr.Timeout = 1000000000;
            //request.ContentType = contentType;

            const string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
            var header = string.Format( headerTemplate, paramName, file, contentType );
            var headerbytes = System.Text.Encoding.UTF8.GetBytes( header );

            var fileStream = new FileStream( file, FileMode.Open, FileAccess.Read );

            const string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";

            var nvcByteCount = 0;
            foreach (string key in nvc.Keys)
            {
                var formitem = string.Format( formdataTemplate, key, nvc[key] );
                var formitembytes = System.Text.Encoding.UTF8.GetBytes( formitem );
                nvcByteCount += formitembytes.Length;
            }
            var trailer = System.Text.Encoding.ASCII.GetBytes( "\r\n--" + boundary + "--\r\n" );

            wr.ContentLength = fileStream.Length + headerbytes.Length + ((nvc.Count + 1) * boundarybytes.Length) + nvcByteCount + trailer.Length;


            var rs = wr.GetRequestStream();


            foreach (string key in nvc.Keys)
            {
                rs.Write( boundarybytes, 0, boundarybytes.Length );
                var formitem = string.Format( formdataTemplate, key, nvc[key] );
                var formitembytes = System.Text.Encoding.UTF8.GetBytes( formitem );
                rs.Write( formitembytes, 0, formitembytes.Length );
            }
            rs.Write( boundarybytes, 0, boundarybytes.Length );

            rs.Write( headerbytes, 0, headerbytes.Length );

            var buffer = new byte[bufferSize];
            int bytesRead;
            var bytesUploaded = 0;
            while ((bytesRead = fileStream.Read( buffer, 0, buffer.Length )) != 0)
            {
                rs.Write( buffer, 0, bytesRead );

                //raise event with upload progress
                bytesUploaded += bytesRead;
                var eventArgs = new UploadProgressEventArgs
                {
                    bytesUploaded = bytesUploaded,
                    bytesTotal = fileStream.Length
                };

                ProgressEvent( this, eventArgs );

            }
            fileStream.Close();

            rs.Write( trailer, 0, trailer.Length );
            rs.Close();

            HttpWebResponse wresp = null;
            var response = "";

            try
            {
                wresp = (HttpWebResponse)wr.GetResponse();
                var stream2 = wresp.GetResponseStream();
                var reader2 = new StreamReader( stream2 );
                response = reader2.ReadToEnd();

            }
            catch (Exception ex)
            {
                Console.Write( ex.ToString() );
                if (wresp != null)
                {
                    wresp.Close();
                }
            }

            return response;
        }