/// <summary>
        /// Create a request to initialize a request.
        /// </summary>
        /// <returns>
        /// An HttpWebRequest configured to initialize a request.
        /// </returns>
        /// <param name="contentLength">Length of the content to be uploaded (in bytes).</param>
        /// <param name="contentType">Content type of the content to be uploaded.</param>
        private HttpWebRequest CreateInitializeRequest(long contentLength, string contentType)
        {
            HttpWebRequestBuilder builder = new HttpWebRequestBuilder()
            {
                BaseUri            = new Uri(this.baseUri),
                Method             = this.httpMethod,
                Path               = this.path,
                HttpRequestFactory = this.RequestFactory,
            };

            builder.AddParameter(RequestParameterType.Query, "uploadType", "resumable");
            SetAllPropertyValues(builder);

            var request = builder.GetWebRequest();

            request.Headers.Add(PayloadContentTypeHeader, contentType);
            request.Headers.Add(PayloadContentLengthHeader, contentLength.ToString());

            if (this.Authenticator != null)
            {
                Authenticator.ApplyAuthenticationToRequest(request);
            }

            string bodyText = this.Serializer.Serialize(this.Body);

            byte[] body = Encoding.UTF8.GetBytes(bodyText);
            request.ContentType   = JsonMimeType;
            request.ContentLength = body.Length;

            var requestBodyStream = request.GetRequestStream();

            requestBodyStream.Write(body, 0, body.Length);
            return(request);
        }
示例#2
0
        /// <summary>
        /// Initializes the resumable upload by calling the resumable rest interface to get
        /// a unique upload Location
        /// </summary>
        /// <returns>
        /// The unique upload location for this upload, returned in the Location header
        /// </returns>
        private string InitializeUpload(Stream payload)
        {
            var request = BuildInitRequest();

            request.ServicePoint.Expect100Continue = false;
            request.Headers.Add("X-Upload-Content-Type", "image/jpeg");
            request.Headers.Add("X-Upload-Content-Length", payload.Length.ToString());
            Authenticator.ApplyAuthenticationToRequest(request);

            var j = new Json.NewtonsoftJsonSerializer();
            var o = j.Serialize(this.GetRequestBody());

            byte[] body = Encoding.UTF8.GetBytes(o);
            request.ContentType   = "application/json";
            request.ContentLength = body.Length;

            var s = request.GetRequestStream();

            s.Write(body, 0, body.Length);

            WebResponse response = request.GetResponse();

            return(response.Headers["Location"]);
        }
示例#3
0
        internal WebRequest CreateWebRequest(Action <WebRequest> onRequestReady)
        {
            HttpWebRequest request = BuildRequest();

            // Create the request.
            Authenticator.ApplyAuthenticationToRequest(request);

            // Insert the content type and user agent.
            request.ContentType = string.Format(
                "{0}; charset={1}", GetReturnMimeType(ReturnType), ContentCharset.WebName);
            string appName     = FormatForUserAgent(ApplicationName);
            string apiVersion  = FormatForUserAgent(ApiVersion);
            string platform    = FormatForUserAgent(Environment.OSVersion.Platform.ToString());
            string platformVer = FormatForUserAgent(Environment.OSVersion.Version.ToString());

            // The UserAgent header can only be set on a non-Silverlight platform.
            // Silverlight uses the user agent of the browser instead.
#if !SILVERLIGHT
            request.UserAgent = String.Format(UserAgent, appName, apiVersion, platform, platformVer);
#endif

            // Add the E-tag header:
            if (!string.IsNullOrEmpty(ETag))
            {
                ETagAction action = this.ETagAction;
                if (action == ETagAction.Default)
                {
                    action = GetDefaultETagAction(request.Method);
                }

                switch (action)
                {
                case ETagAction.IfMatch:
                    request.Headers[HttpRequestHeader.IfMatch] = ETag;
                    break;

                case ETagAction.IfNoneMatch:
                    request.Headers[HttpRequestHeader.IfNoneMatch] = ETag;
                    break;
                }
            }

            // Check if compression is supported.
#if !SILVERLIGHT
            if (Service.GZipEnabled)
            {
                request.UserAgent += GZipUserAgentSuffix;
                request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            }
#endif

            // Attach a body if a POST and there is something to attach.
            if (HttpMethodHasBody(request.Method))
            {
                if (!string.IsNullOrEmpty(Body))
                {
                    AttachBody(request, onRequestReady);
                    return(request);
                }
                else
                {
                    // Set the "Content-Length" header, which is required for every http method declaring a body. This
                    // is required as e.g. the google servers will throw a "411 - Length required" error otherwise.
#if !SILVERLIGHT
                    request.ContentLength = 0;
#else
                    // Set by the browser on Silverlight. Cannot be modified by the user.
#endif
                }
            }

            onRequestReady(request);
            return(request);
        }