Пример #1
0
        private async Task <HttpWebRequest> BuildRequest()
        {
            var _url = Url;

            if (Query != null)
            {
                _url += NetPath.QueryString(Query);
            }

            var request = WebRequest.CreateHttp(_url);

            request.Method      = Method;
            request.KeepAlive   = true;
            request.ContentType = ContentType;

            if (!string.IsNullOrEmpty(Username))
            {
                var creds   = Encoding.ASCII.GetBytes($"{Username}:{Password}");
                var creds64 = Convert.ToBase64String(creds);
                request.Headers[HttpRequestHeader.Authorization] = $"Basic {creds64}";
            }

            var requestBody = Body ?? BodyFunc?.Invoke();

            if (requestBody != null)
            {
                request.ContentLength = requestBody.Length - requestBody.Position;

                try {
                    using (var requestStream = request.GetRequestStream()) {
                        await requestBody.CopyToAsync(requestStream);
                    }
                }
                catch (WebException error) {
                    throw new HttpStatusCodeException(HttpStatusCode.NotFound, error.Message);
                }
            }
            else
            {
                request.ContentLength = 0;
            }

            return(request);
        }
Пример #2
0
        private async Task <HttpWebRequest> BuildRequest()
        {
            var _url = Url;

            if (Query != null)
            {
                _url += NetPath.QueryString(Query);
            }

            var request = WebRequest.CreateHttp(_url);

            request.Method      = Method;
            request.KeepAlive   = true;
            request.ContentType = ContentType;

            var requestBody = Body ?? BodyFunc?.Invoke();

            if (requestBody != null)
            {
                request.ContentLength = requestBody.Length - requestBody.Position;

                try {
                    using (var requestStream = request.GetRequestStream()) {
                        await requestBody.CopyToAsync(requestStream);
                    }
                }
                catch (WebException error) {
                    throw new HttpStatusCodeException(HttpStatusCode.NotFound, error.Message);
                }
            }
            else
            {
                request.ContentLength = 0;
            }

            return(request);
        }