示例#1
0
        /// <summary>
        /// Creates the HttpWebRequest and configures the end point, content, user agent and proxy settings.
        /// </summary>
        /// <param name="requestContext">The async request.</param>
        /// <returns>The web request that actually makes the call.</returns>
        protected virtual IHttpRequest <TRequestContent> CreateWebRequest(IRequestContext requestContext)
        {
            IRequest request     = requestContext.Request;
            Uri      url         = AmazonServiceClient.ComposeUrl(request);
            var      httpRequest = _requestFactory.CreateHttpRequest(url);

            httpRequest.ConfigureRequest(requestContext);

            httpRequest.Method = request.HttpMethod;
            if (request.MayContainRequestBody())
            {
                var content = request.Content;
                if (request.SetContentFromParameters || (content == null && request.ContentStream == null))
                {
                    // Mapping parameters to query string or body are mutually exclusive.
                    if (!request.UseQueryString)
                    {
                        string queryString = AWSSDKUtils.GetParametersAsString(request.Parameters);
                        content         = Encoding.UTF8.GetBytes(queryString);
                        request.Content = content;
                        request.SetContentFromParameters = true;
                    }
                    else
                    {
                        request.Content = new Byte[0];
                    }
                }

                if (content != null)
                {
                    request.Headers[HeaderKeys.ContentLengthHeader] =
                        content.Length.ToString(CultureInfo.InvariantCulture);
                }
                else if (request.ContentStream != null && !request.Headers.ContainsKey(HeaderKeys.ContentLengthHeader))
                {
                    request.Headers[HeaderKeys.ContentLengthHeader] =
                        request.ContentStream.Length.ToString(CultureInfo.InvariantCulture);
                }
            }

            if (requestContext.Unmarshaller is JsonResponseUnmarshaller)
            {
                // Currently the signature seems to be valid without including this header in the calculation.
                request.Headers["Accept"] = "application/json";
            }

            return(httpRequest);
        }
示例#2
0
        /// <summary>
        /// Creates the HttpWebRequest and configures the end point, content, user agent and proxy settings.
        /// </summary>
        /// <param name="requestContext">The async request.</param>
        /// <returns>The web request that actually makes the call.</returns>
        protected virtual IHttpRequest <TRequestContent> CreateWebRequest(IRequestContext requestContext)
        {
            IRequest request     = requestContext.Request;
            Uri      url         = AmazonServiceClient.ComposeUrl(request);
            var      httpRequest = _requestFactory.CreateHttpRequest(url);

            httpRequest.ConfigureRequest(requestContext);

            httpRequest.Method = request.HttpMethod;
            if (request.MayContainRequestBody())
            {
                var content = request.Content;
                if (request.SetContentFromParameters || (content == null && request.ContentStream == null))
                {
                    // Mapping parameters to query string or body are mutually exclusive.
                    if (!request.UseQueryString)
                    {
                        string queryString = AWSSDKUtils.GetParametersAsString(request);
                        content         = Encoding.UTF8.GetBytes(queryString);
                        request.Content = content;
                        request.SetContentFromParameters = true;
                    }
                    else
                    {
                        request.Content = new Byte[0];
                    }
                }

                if (content != null)
                {
                    request.Headers[HeaderKeys.ContentLengthHeader] =
                        content.Length.ToString(CultureInfo.InvariantCulture);
                }
                else if (request.ContentStream != null && request.ContentStream.CanSeek && !request.Headers.ContainsKey(HeaderKeys.ContentLengthHeader))
                {
                    request.Headers[HeaderKeys.ContentLengthHeader] =
                        request.ContentStream.Length.ToString(CultureInfo.InvariantCulture);
                }
            }

            return(httpRequest);
        }
        public async Task SendEventAsync(PublicationMethod publicationMethod,
                                         string hubName,
                                         string hubMethodName,
                                         string[] receiverIds,
                                         object e
                                         )
        {
            foreach (var receiverId in receiverIds ?? _nullReceiver)
            {
                var url = _urlProvider.GetUrl(
                    _connectionString.Endpoint,
                    publicationMethod,
                    hubName,
                    receiverId
                    );

                var request = _httpRequestFactory.CreateHttpRequest(
                    _connectionString,
                    hubMethodName,
                    e,
                    url
                    );

                using (var response = await _signalRHttpClient.SendAsync(request).ConfigureAwait(false))
                {
                    try
                    {
                        response.EnsureSuccessStatusCode();
                    }
                    catch (HttpRequestException ex)
                    {
                        throw new AzureSignalRPublishingFailedException(ex);
                    }

                    if (response.StatusCode != HttpStatusCode.Accepted)
                    {
                        throw new AzureSignalRPublishingFailedException(response.StatusCode);
                    }
                }
            }
        }
示例#4
0
        protected virtual IHttpRequest <TRequestContent> CreateWebRequest(IRequestContext requestContext)
        {
            IRequest request    = requestContext.Request;
            Uri      requestUri = AmazonServiceClient.ComposeUrl(request);
            IHttpRequest <TRequestContent> httpRequest = _requestFactory.CreateHttpRequest(requestUri);

            httpRequest.ConfigureRequest(requestContext);
            httpRequest.Method = request.HttpMethod;
            if (request.MayContainRequestBody())
            {
                byte[] array = request.Content;
                if (request.SetContentFromParameters || (array == null && request.ContentStream == null))
                {
                    if (!request.UseQueryString)
                    {
                        string parametersAsString = AWSSDKUtils.GetParametersAsString(request.Parameters);
                        array = (request.Content = Encoding.UTF8.GetBytes(parametersAsString));
                        request.SetContentFromParameters = true;
                    }
                    else
                    {
                        request.Content = new byte[0];
                    }
                }
                if (array != null)
                {
                    request.Headers["Content-Length"] = array.Length.ToString(CultureInfo.InvariantCulture);
                }
                else if (request.ContentStream != null && !request.Headers.ContainsKey("Content-Length"))
                {
                    request.Headers["Content-Length"] = request.ContentStream.Length.ToString(CultureInfo.InvariantCulture);
                }
            }
            if (requestContext.Unmarshaller is JsonResponseUnmarshaller)
            {
                request.Headers["Accept"] = "application/json";
            }
            return(httpRequest);
        }
        /// <summary>
        /// Creates the HttpWebRequest and configures the end point, content, user agent and proxy settings.
        /// </summary>
        /// <param name="requestContext">The async request.</param>
        /// <returns>The web request that actually makes the call.</returns>
        protected virtual IHttpRequest <TRequestContent> CreateWebRequest(IRequestContext requestContext)
        {
            IRequest request     = requestContext.Request;
            Uri      url         = AliyunServiceClient.ComposeUrl(request);
            var      httpRequest = _requestFactory.CreateHttpRequest(url);

            httpRequest.ConfigureRequest(requestContext);

            httpRequest.Method = request.HttpMethod;
            if (request.MayContainRequestBody())
            {
                if (request.Content == null && (request.ContentStream == null))
                {
                    string queryString = AliyunSDKUtils.GetParametersAsString(request.Parameters);
                    request.Content = Encoding.UTF8.GetBytes(queryString);
                }

                if (request.Content != null)
                {
                    request.Headers[HttpHeader.ContentLengthHeader] =
                        request.Content.Length.ToString(CultureInfo.InvariantCulture);
                }
                else if (request.ContentStream != null && !request.Headers.ContainsKey(HttpHeader.ContentLengthHeader))
                {
                    request.Headers[HttpHeader.ContentLengthHeader] =
                        request.ContentStream.Length.ToString(CultureInfo.InvariantCulture);
                }
            }
            else if (request.UseQueryString &&
                     (request.HttpMethod == "POST" ||
                      request.HttpMethod == "PUT" ||
                      request.HttpMethod == "DELETE"))
            {
                request.Content = new Byte[0];
            }

            return(httpRequest);
        }
        public void Test()
        {
            var proxyCreds   = new System.Net.NetworkCredential("UserName", "Password");
            var clientConfig = new AmazonS3Config
            {
                ConnectionLimit   = 10,
                MaxIdleTime       = 1000,
                ProxyCredentials  = proxyCreds,
                ProxyHost         = "proxyhost",
                ProxyPort         = 8080,
                ReadWriteTimeout  = TimeSpan.FromSeconds(20),
                Timeout           = TimeSpan.FromSeconds(40),
                UseNagleAlgorithm = false,
                RegionEndpoint    = RegionEndpoint.USEast1
            };

            var requestFactory = new HttpRequestMessageFactory(clientConfig);
            IHttpRequestFactory <HttpContent> factory = requestFactory;
            var request = factory.CreateHttpRequest(new Uri(@"https://testuri"));

            Assert.IsNotNull(request);
            request.Method = "PUT";
            var httpWebRequestMessage = ((HttpWebRequestMessage)request);
            var httpRequestMessage    = httpWebRequestMessage.Request;

            Assert.AreEqual(TimeSpan.FromSeconds(40), httpWebRequestMessage.HttpClient.Timeout);
            Assert.AreEqual(false, httpWebRequestMessage.HttpClient.DefaultRequestHeaders.ExpectContinue);
            Assert.AreEqual(HttpMethod.Put, httpRequestMessage.Method);

            var putObjectRequest = new PutObjectRequest
            {
                BucketName  = "TestBucket",
                Key         = "TestKey",
                ContentBody = "Test_Content",
            };

            var requestContext = new RequestContext(true)
            {
                ClientConfig    = clientConfig,
                Marshaller      = new PutObjectRequestMarshaller(),
                OriginalRequest = putObjectRequest,
                Request         = new PutObjectRequestMarshaller().Marshall(putObjectRequest),
                Unmarshaller    = new PutObjectResponseUnmarshaller()
            };

            request.ConfigureRequest(requestContext);

            var date    = DateTime.Now.ToUniversalTime();
            var headers = new Dictionary <string, string>
            {
                { "Accept", "text/plain" },
                //{"Connection","Keep-Alive"},
                { "Content-Type", "application/json" },
                { "Content-Length", "100" },
                //{"Expect","100-continue"},
                { "User-Agent", "awssdk" },
                { "Date", date.ToString("r") },
                { "Host", "s3.amazonaws.com" },
                { "Range", "bytes=100-200" },
                { "Content-Range", "bytes 100-300/*" },
                { "If-Modified-Since", date.ToString("r") },
                { "Expires", date.ToString("r") },
                { "NonStandardHeader", "TestValue" },
            };

            request.SetRequestHeaders(headers);

            var sourceStream      = new MemoryStream(Encoding.UTF8.GetBytes("Test_Content"));
            var length            = sourceStream.Length;
            var destinationStream = httpRequestMessage.Content;

            request.WriteToRequestBody(destinationStream, sourceStream, headers, requestContext);
            Assert.AreEqual(length, httpRequestMessage.Content.Headers.ContentLength);

            var sourceContent = Encoding.UTF8.GetBytes("Test_Content123");

            destinationStream = httpRequestMessage.Content;
            request.WriteToRequestBody(destinationStream, sourceContent, headers);
            Assert.AreEqual(sourceContent.Length, httpRequestMessage.Content.Headers.ContentLength);

            Assert.AreEqual("text/plain", httpRequestMessage.Headers.Accept.First().MediaType);
            Assert.AreEqual("application/json", httpRequestMessage.Content.Headers.ContentType.MediaType);
            Assert.AreEqual("awssdk", httpRequestMessage.Headers.UserAgent.First().Product.Name);
            Assert.AreEqual(DateTime.Parse(date.ToString("r")), httpRequestMessage.Headers.Date);
            Assert.AreEqual("s3.amazonaws.com", httpRequestMessage.Headers.Host);
            Assert.AreEqual(DateTime.Parse(date.ToString("r")), httpRequestMessage.Headers.IfModifiedSince);
            Assert.AreEqual(DateTime.Parse(date.ToString("r")), httpRequestMessage.Content.Headers.Expires);
            Assert.AreEqual("bytes", httpRequestMessage.Headers.Range.Unit);
            Assert.AreEqual(100, httpRequestMessage.Headers.Range.Ranges.First().From);
            Assert.AreEqual(200, httpRequestMessage.Headers.Range.Ranges.First().To);
            Assert.AreEqual("bytes", httpRequestMessage.Content.Headers.ContentRange.Unit);
            Assert.AreEqual(100, httpRequestMessage.Content.Headers.ContentRange.From);
            Assert.AreEqual(300, httpRequestMessage.Content.Headers.ContentRange.To);
            Assert.AreEqual("TestValue", httpRequestMessage.Headers.GetValues("NonStandardHeader").First());
        }