Beispiel #1
0
        private string executeRequest(string url, string method, string data)
        {
            var request = (HttpWebRequest)WebRequest.Create(url);

            request.Method = method;

            //sign the request
            var consumer = new OAuth.OAuthConsumer();

            consumer.SetTokenWithSecret(username, token);
            request = consumer.Sign(request);


            switch (method.ToUpper())
            {
            case "GET":
                break;

            case "POST":
                request.ContentType   = "application/xml";
                request.ContentLength = Encoding.UTF8.GetBytes(data).Length;

                var requestStream = request.GetRequestStream();
                requestStream.Write(Encoding.UTF8.GetBytes(data), 0, Encoding.UTF8.GetBytes(data).Length);
                requestStream.Close();
                break;

            case "PUT":
                request.ContentType   = "application/xml";
                request.ContentLength = Encoding.UTF8.GetBytes(data).Length;

                var reqS = request.GetRequestStream();
                reqS.Write(Encoding.UTF8.GetBytes(data), 0, Encoding.UTF8.GetBytes(data).Length);
                reqS.Close();
                break;

            case "DELETE":
                request.ContentType   = "application/xml";
                request.KeepAlive     = false;
                request.ContentLength = Encoding.UTF8.GetBytes(data).Length;

                var rs = request.GetRequestStream();
                rs.Write(Encoding.UTF8.GetBytes(data), 0, Encoding.UTF8.GetBytes(data).Length);
                rs.Close();
                break;

            default:
                throw new Exception("HTTP Method " + method + " is not supported");
            }

            WebResponse response    = null;
            var         rawResponse = String.Empty;

            try
            {
                response = request.GetResponse();
                Debug.WriteLine(((HttpWebResponse)response).StatusDescription);
                var reader = new StreamReader(response.GetResponseStream());
                rawResponse = reader.ReadToEnd();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                throw;
            }

            return(rawResponse);
        }
Beispiel #2
0
        private string executeRequest ( string url, string method, string data )
        {
            var request = (HttpWebRequest)WebRequest.Create( url );
            request.Method = method;

            //sign the request
            var consumer = new OAuth.OAuthConsumer();
            consumer.SetTokenWithSecret( username, token );
            request = consumer.Sign( request );


            switch (method.ToUpper())
            {
                case "GET":
                    break;

                case "POST":
                    request.ContentType = "application/xml";
                    request.ContentLength = Encoding.UTF8.GetBytes( data ).Length;

                    var requestStream = request.GetRequestStream();
                    requestStream.Write( Encoding.UTF8.GetBytes( data ), 0, Encoding.UTF8.GetBytes( data ).Length );
                    requestStream.Close();
                    break;

                case "PUT":
                    request.ContentType = "application/xml";
                    request.ContentLength = Encoding.UTF8.GetBytes(data).Length;

                    var reqS = request.GetRequestStream();
                    reqS.Write(Encoding.UTF8.GetBytes(data), 0, Encoding.UTF8.GetBytes(data).Length);
                    reqS.Close();
                    break;

                case "DELETE":
                    request.ContentType = "application/xml";
                    request.KeepAlive = false;
                    request.ContentLength = Encoding.UTF8.GetBytes( data ).Length;

                    var rs = request.GetRequestStream();
                    rs.Write( Encoding.UTF8.GetBytes( data ), 0, Encoding.UTF8.GetBytes( data ).Length );
                    rs.Close();
                    break;

                default:
                    throw new Exception( "HTTP Method " + method + " is not supported" );
            }

            WebResponse response = null;
            var rawResponse = String.Empty;

            try
            {
                response = request.GetResponse();
                var reader = new StreamReader( response.GetResponseStream() );
                rawResponse = reader.ReadToEnd();

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return rawResponse;
        }
Beispiel #3
0
        public string uploadThumbnail(Int64 videoId, string path)
        {
            var url     = apiUrl + "/api/videos/" + videoId + "/upload_thumb.json";
            var result  = String.Empty;
            var request = (HttpWebRequest)WebRequest.Create(url);

            request.Method = "POST";

            var consumer = new OAuth.OAuthConsumer();

            consumer.SetTokenWithSecret(username, token);
            request = consumer.Sign(request);

            var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);

            request.ContentType = "multipart/form-data; boundary=" + boundary;
            boundary            = "--" + boundary;

            var rs = request.GetRequestStream();

            var buff = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);

            rs.Write(buff, 0, buff.Length);
            buff = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", "vzaar-api[thumbnail]", path, Environment.NewLine));
            rs.Write(buff, 0, buff.Length);
            buff = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", "image/jpeg", Environment.NewLine));
            rs.Write(buff, 0, buff.Length);

            var imgStream = File.Open(path, FileMode.Open);

            imgStream.CopyTo(rs);

            buff = Encoding.ASCII.GetBytes(Environment.NewLine);
            rs.Write(buff, 0, buff.Length);

            var boundaryBuffer = Encoding.ASCII.GetBytes(boundary + "--");

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

            rs.Close();

            WebResponse response    = null;
            var         rawResponse = String.Empty;

            try
            {
                response = request.GetResponse();
                Debug.WriteLine(((HttpWebResponse)response).StatusDescription);
                var reader = new StreamReader(response.GetResponseStream());
                rawResponse = reader.ReadToEnd();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                throw;
            }
            if (rawResponse.Length > 0)
            {
                JObject resp = JObject.Parse(rawResponse);
                result = resp["vzaar-api"]["status"].ToString();
            }

            return(result);
        }
Beispiel #4
0
		public string uploadThumbnail(Int64 videoId, string path)
		{
			var url = apiUrl + "/api/videos/" + videoId + "/upload_thumb.json";
			var result = String.Empty;
			var request = (HttpWebRequest)WebRequest.Create(url);
			request.Method = "POST";

			var consumer = new OAuth.OAuthConsumer();
			consumer.SetTokenWithSecret(username, token);
			request = consumer.Sign(request);

			var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
			request.ContentType = "multipart/form-data; boundary=" + boundary;
			boundary = "--" + boundary;

			var rs = request.GetRequestStream();

			var buff = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
			rs.Write(buff, 0, buff.Length);
			buff = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", "vzaar-api[thumbnail]", path, Environment.NewLine));
			rs.Write(buff, 0, buff.Length);
			buff = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", "image/jpeg", Environment.NewLine));
			rs.Write(buff, 0, buff.Length);

			var imgStream = File.Open(path, FileMode.Open);
			imgStream.CopyTo(rs);

			buff = Encoding.ASCII.GetBytes(Environment.NewLine);
			rs.Write(buff, 0, buff.Length);

			var boundaryBuffer = Encoding.ASCII.GetBytes(boundary + "--");
			rs.Write(boundaryBuffer, 0, boundaryBuffer.Length);

			rs.Close();

			WebResponse response = null;
			var rawResponse = String.Empty;

			try
			{
				response = request.GetResponse();
				var reader = new StreamReader(response.GetResponseStream());
				rawResponse = reader.ReadToEnd();

			}
			catch (Exception ex)
			{
			    result = null;
			}
			if (rawResponse.Length > 0)
			{
				JObject resp = JObject.Parse(rawResponse);
				result = resp["vzaar-api"]["status"].ToString();
			}

			return result;
		}