示例#1
0
        public HttpWebResponse GetResponse(AsyncHttpRequest req, object requestdata = null)
        {
            if (requestdata != null)
            {
                if (requestdata is System.IO.Stream)
                {
                    var stream = requestdata as System.IO.Stream;
                    req.Request.ContentLength = stream.Length;
                    if (string.IsNullOrEmpty(req.Request.ContentType))
                    {
                        req.Request.ContentType = "application/octet-stream";
                    }

                    using (var rs = req.GetRequestStream())
                        Library.Utility.Utility.CopyStream(stream, rs);
                }
                else
                {
                    var data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(requestdata));
                    req.Request.ContentLength = data.Length;
                    req.Request.ContentType   = "application/json; charset=UTF-8";

                    using (var rs = req.GetRequestStream())
                        rs.Write(data, 0, data.Length);
                }
            }

            return((HttpWebResponse)req.GetResponse());
        }
示例#2
0
        public HttpWebResponse GetResponseWithoutException(AsyncHttpRequest req)
        {
            try
            {
                return((HttpWebResponse)req.GetResponse());
            }
            catch (WebException wex)
            {
                if (wex.Response is HttpWebResponse)
                {
                    return((HttpWebResponse)wex.Response);
                }

                throw;
            }
        }
示例#3
0
        public void Get(string remotename, System.IO.Stream stream)
        {
            // Prevent repeated download url lookups
            if (m_filecache.Count == 0)
            {
                List();
            }

            var fileid = GetFileId(remotename);

            var req  = m_oauth.CreateRequest(string.Format("{0}/files/{1}?alt=media", DRIVE_API_URL, fileid));
            var areq = new AsyncHttpRequest(req);

            using (var resp = (HttpWebResponse)areq.GetResponse())
                using (var rs = resp.GetResponseStream())
                    Duplicati.Library.Utility.Utility.CopyStream(rs, stream);
        }
示例#4
0
    public void Get(string remotename, System.IO.Stream stream)
    {
        // Prevent repeated download url lookups
        if (m_filecache.Count == 0)
        {
            foreach (var file in List()) /* Enumerate the full listing */ } {
    }

    var fileId = GetFileEntries(remotename).OrderByDescending(x => x.createdDate).First().id;

    var req  = m_oauth.CreateRequest(WebApi.GoogleDrive.GetUrl(fileId));
    var areq = new AsyncHttpRequest(req);

    using (var resp = (HttpWebResponse)areq.GetResponse())
        using (var rs = areq.GetResponseStream())
            Duplicati.Library.Utility.Utility.CopyStream(rs, stream);
}
示例#5
0
    public void Get(string remotename, System.IO.Stream stream)
    {
        // Prevent repeated download url lookups
        if (m_filecache.Count == 0)
        {
            foreach (var file in List()) /* Enumerate the full listing */ } {
    }

    var fileid = GetFileEntries(remotename).OrderByDescending(x => x.createdDate).First().id;

    var req  = m_oauth.CreateRequest(string.Format("{0}/files/{1}?alt=media{2}", DRIVE_API_URL, fileid, m_useTeamDrive ? "&supportsTeamDrives=true" : string.Empty));
    var areq = new AsyncHttpRequest(req);

    using (var resp = (HttpWebResponse)areq.GetResponse())
        using (var rs = areq.GetResponseStream())
            Duplicati.Library.Utility.Utility.CopyStream(rs, stream);
}
示例#6
0
        /// <summary>
        /// Uploads the requestdata as JSON and starts a resumeable upload session, then uploads the stream in chunks
        /// </summary>
        /// <returns>Serialized result of the last request.</returns>
        /// <param name="oauth">The Oauth instance.</param>
        /// <param name="requestdata">The data to submit as JSON metadata.</param>
        /// <param name="url">The URL to register the upload session against.</param>
        /// <param name="stream">The stream with content data to upload.</param>
        /// <param name="method">The HTTP Method.</param>
        /// <typeparam name="TRequest">The type of data to upload as metadata.</typeparam>
        /// <typeparam name="TResponse">The type of data returned from the upload.</typeparam>
        public static async Task <TResponse> ChunkedUploadWithResumeAsync <TRequest, TResponse>(OAuthHelper oauth, TRequest requestdata, string url, System.IO.Stream stream, CancellationToken cancelToken, string method = "POST")
            where TRequest : class
            where TResponse : class
        {
            var data = requestdata == null ? null : System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(requestdata));

            var req = oauth.CreateRequest(url);

            req.Method        = method;
            req.ContentLength = data == null ? 0 : data.Length;

            if (data != null)
            {
                req.ContentType = "application/json; charset=UTF-8";
            }

            req.Headers["X-Upload-Content-Type"]   = "application/octet-stream";
            req.Headers["X-Upload-Content-Length"] = stream.Length.ToString();

            var areq = new AsyncHttpRequest(req);

            if (data != null)
            {
                using (var rs = areq.GetRequestStream())
                    await rs.WriteAsync(data, 0, data.Length, cancelToken).ConfigureAwait(false);
            }

            string uploaduri;

            using (var resp = (HttpWebResponse)areq.GetResponse())
            {
                if (resp.StatusCode != HttpStatusCode.OK || string.IsNullOrWhiteSpace(resp.Headers["Location"]))
                {
                    throw new WebException("Failed to start upload session", null, WebExceptionStatus.UnknownError, resp);
                }

                uploaduri = resp.Headers["Location"];
            }

            return(await ChunkedUploadAsync <TResponse>(oauth, uploaduri, stream, cancelToken).ConfigureAwait(false));
        }
示例#7
0
        public async Task <HttpWebResponse> GetResponseWithoutExceptionAsync(AsyncHttpRequest req, CancellationToken cancelToken, object requestdata = null)
        {
            try
            {
                if (requestdata != null)
                {
                    if (requestdata is System.IO.Stream stream)
                    {
                        req.Request.ContentLength = stream.Length;
                        if (string.IsNullOrEmpty(req.Request.ContentType))
                        {
                            req.Request.ContentType = "application/octet-stream";
                        }

                        using (var rs = req.GetRequestStream())
                            await Utility.Utility.CopyStreamAsync(stream, rs, cancelToken).ConfigureAwait(false);
                    }
                    else
                    {
                        var data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(requestdata));
                        req.Request.ContentLength = data.Length;
                        req.Request.ContentType   = "application/json; charset=UTF-8";

                        using (var rs = req.GetRequestStream())
                            await rs.WriteAsync(data, 0, data.Length, cancelToken).ConfigureAwait(false);
                    }
                }

                return((HttpWebResponse)req.GetResponse());
            }
            catch (WebException wex)
            {
                if (wex.Response is HttpWebResponse response)
                {
                    return(response);
                }

                throw;
            }
        }
示例#8
0
        public HttpWebResponse GetResponseWithoutException(AsyncHttpRequest req, object requestdata = null)
        {
            try
            {
                if (requestdata != null)
                {
                    if (requestdata is Stream stream)
                    {
                        req.Request.ContentLength = stream.Length;
                        if (string.IsNullOrEmpty(req.Request.ContentType))
                        {
                            req.Request.ContentType = "application/octet-stream";
                        }

                        using (var rs = req.GetRequestStream())
                            Library.Utility.Utility.CopyStream(stream, rs);
                    }
                    else
                    {
                        var data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(requestdata));
                        req.Request.ContentLength = data.Length;
                        req.Request.ContentType   = "application/json; charset=UTF-8";

                        using (var rs = req.GetRequestStream())
                            rs.Write(data, 0, data.Length);
                    }
                }

                return((HttpWebResponse)req.GetResponse());
            }
            catch (WebException wex)
            {
                if (wex.Response is HttpWebResponse response)
                {
                    return(response);
                }

                throw;
            }
        }
示例#9
0
文件: Sia.cs 项目: xilutian/duplicati
        public Task PutAsync(string remotename, string filename, CancellationToken cancelToken)
        {
            string endpoint = "";
            string siafile  = m_targetpath + "/" + remotename;

            try {
                endpoint = string.Format("/renter/upload/{0}/{1}?source={2}",
                                         m_targetpath,
                                         Utility.Uri.UrlEncode(remotename).Replace("+", "%20"),
                                         Utility.Uri.UrlEncode(filename).Replace("+", "%20")
                                         );

                HttpWebRequest req = CreateRequest(endpoint);
                req.Method = WebRequestMethods.Http.Post;

                AsyncHttpRequest areq = new AsyncHttpRequest(req);

                using (HttpWebResponse resp = (HttpWebResponse)areq.GetResponse())
                {
                    int code = (int)resp.StatusCode;
                    if (code < 200 || code >= 300)
                    {
                        throw new WebException(resp.StatusDescription, null, WebExceptionStatus.ProtocolError, resp);
                    }

                    while (!IsUploadComplete(siafile))
                    {
                        Thread.Sleep(5000);
                    }
                }
            }
            catch (WebException wex)
            {
                throw new Exception(getResponseBodyOnError(endpoint, wex));
            }

            return(Task.FromResult(true));
        }
示例#10
0
        public void Get(string remotename, System.IO.Stream stream)
        {
            try
            {
                var url  = WebApi.GoogleCloudStorage.GetUrl(m_bucket, Library.Utility.Uri.UrlPathEncode(m_prefix + remotename));
                var req  = m_oauth.CreateRequest(url);
                var areq = new AsyncHttpRequest(req);

                using (var resp = areq.GetResponse())
                    using (var rs = areq.GetResponseStream())
                        Library.Utility.Utility.CopyStream(rs, stream);
            }
            catch (WebException wex)
            {
                if (wex.Response is HttpWebResponse && ((HttpWebResponse)wex.Response).StatusCode == HttpStatusCode.NotFound)
                {
                    throw new FileMissingException();
                }
                else
                {
                    throw;
                }
            }
        }
示例#11
0
        public void Get(string remotename, System.IO.Stream stream)
        {
            try
            {
                var url  = string.Format("{0}/b/{1}/o/{2}?alt=media", API_URL, m_bucket, Library.Utility.Uri.UrlPathEncode(m_prefix + remotename));
                var req  = m_oauth.CreateRequest(url);
                var areq = new AsyncHttpRequest(req);

                using (var resp = areq.GetResponse())
                    using (var rs = resp.GetResponseStream())
                        Library.Utility.Utility.CopyStream(rs, stream);
            }
            catch (WebException wex)
            {
                if (wex.Response is HttpWebResponse && ((HttpWebResponse)wex.Response).StatusCode == HttpStatusCode.NotFound)
                {
                    throw new FileMissingException();
                }
                else
                {
                    throw;
                }
            }
        }