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


C# IRequestFactory.Create方法代码示例

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


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

示例1: CreateRequest

        internal static WebRequest CreateRequest(RequestInfoBase info, IRequestFactory requestFactory)
        {
            WebRequest Request = requestFactory.Create(info.Uri.AbsoluteUri);

            Request.Method = info.Method.ToString();
            if ((info.Method == RequestMethod.POST || info.Method == RequestMethod.PUT))
            {
                Request.ContentType = info.RequestContentType.ToHeaderValue();
            }



            if (Request is HttpWebRequest)
            {
                var request = ((HttpWebRequest)Request);

                switch (info.ResponseContentType)
                {
                    case ContentType.JSON:
                        request.Accept = "text/plain, text/json, application/json";
                        break;
                    case ContentType.FORM:
                        request.Accept = "text/plain, text/json, application/json, text/xml, application/xml";
                        break;
                    case ContentType.XML:
                        request.Accept = "text/xml, application/xml";
                        break;
                    case ContentType.TEXT:
                        request.Accept = "text/plain";
                        break;
                }

                request.UserAgent = info.UserAgent;
                var headers = info.Headers;
                if (headers != null)
                {
                    foreach (var header in headers)
                    {
                        request.Headers[header.Key] = header.Value.ToString();

                    }
                }
            }


            if ((info.Method == RequestMethod.POST || info.Method == RequestMethod.PUT))
            {
                if (string.IsNullOrEmpty(info.RequestBody))
                {
#if !WINDOWS_PHONE
                    Request.ContentLength = 0;
#endif
                }
                else
                {
                    // set request stream
                    var gate = new AutoResetEvent(false);
                    byte[] bodyValue = Encoding.UTF8.GetBytes(info.RequestBody);
                    Exception exception = null;
                    Request.BeginGetRequestStream(ac =>
                    {
                        try
                        {
                            using (
                                Stream requestStream =
                                    Request.EndGetRequestStream(ac))
                            {
                                requestStream.Write(bodyValue, 0, bodyValue.Length);
                                requestStream.Flush();
                            }
                        }
                        catch (Exception ex)
                        {
                            exception = ex;
                        }
                        finally
                        {
                            gate.Set();
                        }
                    }, null);

                    gate.WaitOne();
                    // #FIXME: logic to catch stalls conflicts with throttle
                    //if (!gate.WaitOne(10000))
                    //{
                    //    throw new Exception("timed out setting request body");
                    //}
                    if (exception != null)
                    {
                        throw exception;
                    }
                }

            }
            return Request;
        }
开发者ID:sopel,项目名称:Salient.ReliableHttpClient,代码行数:96,代码来源:RequestInfoBase.cs


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