Пример #1
0
        private void OnUploadProgressCompleted(object sender, UploadFileCompletedEventArgs e)
        {
            // Displays the operation identifier, and the transfer progress.
            //--Console.WriteLine("{0}    uploaded {1} of {2} bytes. {3} % complete...", (string)e.UserState, e.Error, e.Result, e.Cancelled);

            var responseBytes = e.Result;

            var responseString = Encoding.UTF8.GetString(responseBytes);

            response2 = JsonConvert.DeserializeObject <UploadResponse>(responseString);

            //ContinueWithPublishing();
        }
Пример #2
0
        private UploadedResponse PublishVideo(UploadResponse uploadResponse, string accessToken)
        {
            //"https://api.dailymotion.com/me/videos?url=http://upload-07.dailymotion.com/files/f4f65be7a2bcc575283a62ec7fe8cdb8.mkv#c2VhbD0wMzE3Y2UxN2Y1YjQ4MDM0OWFhMDI0YzhmMTY4ODAzYyZhY29kZWM9RkxBQyZiaXRyYXRlPTYxMjEyODYmZGltZW5zaW9uPTMyMDB4MTgwMCZkdXJhdGlvbj0xNTQ1MjM0JmZvcm1hdD1NYXRyb3NrYSZoYXNoPTRjNmE3YjU1MDQ1NGZjZjEwN2NiN2MwNGUwYjM4YWJlZTMzMjg0OGImbmFtZT1nb3RoaWNfMDAxLW11eGVkJnNpemU9MTE4MjM1MjQxNCZzdHJlYW1hYmxlPSZ2Y29kZWM9QVZD"
            var request = WebRequest.Create("https://api.dailymotion.com/me/videos?url=" + HttpUtility.UrlEncode(uploadResponse.url));

            request.Method      = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Headers.Add("Authorization", "OAuth " + accessToken);

            var requestString = GetPostStringWithOutDescription();

            var requestBytes = Encoding.UTF8.GetBytes(requestString);

            var requestStream = request.GetRequestStream();

            //byte[] buffer = new byte[4 * 1024];
            //int bytesRead = 0;
            //while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            //{
            //	try
            //	{
            //		requestStream.Write(buffer, 0, bytesRead);
            //		BytesTransferred += bytesRead;
            //		Percentage = (double)fileStream.Position / lenghtOfBytes * 100;
            //		//--Console.WriteLine("{0} von {1} Bytes hochgeladen.", fileStream.Position, fileStream.Length);
            //	}
            //	catch (Exception ex)
            //	{
            //		Console.WriteLine(ex.Message);
            //	}
            //}

            int position = 0;

            while (position < requestBytes.Length)
            {
                //byte[] sendBytes = new byte[(position < requestBytes.Length - 4*1024)?4*1024:requestBytes.Length - position];
                requestStream.Write(requestBytes, position, (position < requestBytes.Length - 4 * 1024) ? 4 * 1024 : requestBytes.Length - position);
                position += (position < requestBytes.Length - 4 * 1024) ? 4 * 1024 : requestBytes.Length - position;
            }

            //requestStream.Write(requestBytes, 0, requestBytes.Length);
            //requestStream.Flush();

            WebResponse response;

            ////accessToken = Authenticator.AccessToken;

            //string somethingstring = GetDescriptionPostString();

            response = request.GetResponse();

            var    responseStream = response.GetResponseStream();
            string responseString;

            using (var reader = new StreamReader(responseStream))
            {
                responseString = reader.ReadToEnd();
            }
            var uploadedResponse = JsonConvert.DeserializeObject <UploadedResponse>(responseString);

            this.VideoId = uploadedResponse.id;

            return(uploadedResponse);
        }
Пример #3
0
        public UploadResponse HttpUploadFile(string accessToken, string url, string file, string paramName, string contentType, NameValueCollection nvc, string type = "Video")
        {
            IsRunning = true;

            //--Console.WriteLine(string.Format("Uploading {0} to {1}", file, url));
            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");

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

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

            wr.Proxy       = null;
            wr.ContentType = "multipart/form-data; boundary=" + boundary;
            wr.Method      = "POST";
            wr.KeepAlive   = true;
            wr.Headers.Add("Authorization", "OAuth " + accessToken);
            wr.Credentials     = System.Net.CredentialCache.DefaultCredentials;
            wr.ProtocolVersion = HttpVersion.Version10;

            wr.ServicePoint.SetTcpKeepAlive(true, 10000, 1000);

            long lenghtOfBytes = 0;

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

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

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

            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
            lenghtOfBytes += headerbytes.Length;

            long fileLength = new FileInfo(file).Length;

            lenghtOfBytes += fileLength;

            byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
            lenghtOfBytes += trailer.Length;

            wr.AllowWriteStreamBuffering = false;
            wr.ContentLength             = lenghtOfBytes;

            //--Console.WriteLine(wr.AuthenticationLevel.ToString());
            wr.Timeout = int.MaxValue;

            Stream rs = wr.GetRequestStream();

            rs.WriteTimeout = -1;

            formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
            foreach (string key in nvc.Keys)
            {
                rs.Write(boundarybytes, 0, boundarybytes.Length);
                lenghtOfBytes += boundarybytes.Length;
                string formitem      = string.Format(formdataTemplate, key, nvc[key]);
                byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                rs.Write(formitembytes, 0, formitembytes.Length);
                lenghtOfBytes += formitembytes.Length;
            }
            rs.Write(boundarybytes, 0, boundarybytes.Length);
            lenghtOfBytes += boundarybytes.Length;

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

            rs.WriteTimeout = int.MaxValue;

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

            this.BytesToTransfer  = fileStream.Length;
            this.BytesTransferred = 0;
            byte[] buffer    = new byte[4 * 1024];
            int    bytesRead = 0;

            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                try
                {
                    rs.Write(buffer, 0, bytesRead);
                    BytesTransferred += bytesRead;
                    Percentage        = (double)fileStream.Position / lenghtOfBytes * 100;
                    Message           = string.Format("Hochladen des {0}s: {1:0.00}% fertig, Hochgeladen: {2:0.00} MB, Hochzuladen: {3:0.00} MB", type, Percentage, BytesTransferred / 1024 / 1024, BytesToTransfer / 1024 / 1024);
                    //--Console.WriteLine("{0} von {1} Bytes hochgeladen.", fileStream.Position, fileStream.Length);
                }
                catch (Exception ex)
                {
                    this.Message   = string.Format("Upload des {0}s fehlgeschlagen: {0}", type, ex.Message);
                    this.Failed    = true;
                    this.IsRunning = false;
                    this.Finished  = true;

                    rs.Close();
                    return(null);
                }
            }
            fileStream.Close();

            trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
            rs.Write(trailer, 0, trailer.Length);
            rs.Close();

            WebResponse wresp = null;

            try
            {
                var          writerresponse = wr.GetResponse();
                Stream       stream2        = writerresponse.GetResponseStream();
                StreamReader reader2        = new StreamReader(stream2);
                var          test           = reader2.ReadToEnd();
                response = JsonConvert.DeserializeObject <UploadResponse>(test);
                //--Console.WriteLine(response.ToString());
                //--Console.WriteLine(string.Format("File uploaded, server response is: {0}", test));
            }
            catch (Exception ex)
            {
                //--Console.WriteLine("Error uploading file", ex);
                this.Message = ex.Message;
                this.Failed  = true;

                if (wresp != null)
                {
                    wresp.Close();
                    wresp = null;
                }
            }
            finally
            {
                wr = null;
            }

            return(response);
        }