当前位置: 首页>>代码示例>>C#>>正文


C# RequestOptions.ApplyRequestModifications方法代码示例

本文整理汇总了C#中RequestOptions.ApplyRequestModifications方法的典型用法代码示例。如果您正苦于以下问题:C# RequestOptions.ApplyRequestModifications方法的具体用法?C# RequestOptions.ApplyRequestModifications怎么用?C# RequestOptions.ApplyRequestModifications使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在RequestOptions的用法示例。


在下文中一共展示了RequestOptions.ApplyRequestModifications方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AssertRequestOptionCall

 private void AssertRequestOptionCall(Action<IRequestOptions> options,
                                      Action<HttpRequestMessage> assertions,
                                      string uri = "http://whatever")
 {
     var requestMessage = new HttpRequestMessage(HttpMethod.Get, uri);
     var requestOptions = new RequestOptions();
     options(requestOptions);
     requestOptions.ApplyRequestModifications(requestMessage);
     assertions(requestMessage);
 }
开发者ID:Pomona,项目名称:Pomona,代码行数:10,代码来源:RequestOptionsTests.cs

示例2: SendHttpRequestAsync

        private async Task<string> SendHttpRequestAsync(ISerializationContextProvider serializationContextProvider,
                                                        string uri,
                                                        string httpMethod,
                                                        object requestBodyEntity,
                                                        TypeSpec requestBodyBaseType,
                                                        RequestOptions options)
        {
            byte[] requestBytes = null;
            HttpResponseMessage response = null;
            if (requestBodyEntity != null)
            {
                requestBytes = this.serializerFactory
                                   .GetSerializer(serializationContextProvider)
                                   .SerializeToBytes(requestBodyEntity, new SerializeOptions
                                   {
                                       ExpectedBaseType = requestBodyBaseType
                                   });
            }
            var request = new HttpRequestMessage(new System.Net.Http.HttpMethod(httpMethod), uri);

            string responseString = null;
            Exception thrownException = null;
            try
            {
                if (options != null)
                    options.ApplyRequestModifications(request);

                AddDefaultHeaders(request);

                const string jsonContentType = "application/json; charset=utf-8";
                request.Headers.Add("Accept", jsonContentType);
                if (requestBytes != null)
                {
                    var requestContent = new ByteArrayContent(requestBytes);
                    requestContent.Headers.ContentType = MediaTypeHeaderValue.Parse(jsonContentType);
                    request.Content = requestContent;
                }

                using (Profiler.Step("client: " + request.Method + " " + request.RequestUri))
                {
                    response = await WebClient.SendAsync(request, CancellationToken.None);
                }

                if (response.Content != null)
                {
                    responseString = await response.Content.ReadAsStringAsync();
                    if (responseString.Length == 0)
                        responseString = null;
                }

                if ((int)response.StatusCode >= 400)
                {
                    var gotJsonResponseBody = responseString != null && response.Content.Headers.ContentType.MediaType == "application/json";

                    var responseObject = gotJsonResponseBody
                        ? Deserialize(responseString, null, serializationContextProvider)
                        : null;

                    throw WebClientException.Create(this.typeMapper, request, response, responseObject, null);
                }
            }
            catch (Exception ex)
            {
                thrownException = ex;
                throw;
            }
            finally
            {
                var eh = RequestCompleted;
                if (eh != null)
                {
                    // Since request content has been disposed at this point we recreate it..
                    if (request.Content != null)
                    {
                        var nonDisposedContent = new ByteArrayContent(requestBytes);
                        nonDisposedContent.Headers.CopyHeadersFrom(request.Content.Headers);
                        request.Content = nonDisposedContent;
                    }
                    eh(this, new ClientRequestLogEventArgs(request, response, thrownException));
                }
            }

            return responseString;
        }
开发者ID:Pomona,项目名称:Pomona,代码行数:84,代码来源:RequestDispatcher.cs


注:本文中的RequestOptions.ApplyRequestModifications方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。