本文整理汇总了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;
}