本文整理汇总了C#中HttpMethod.GetBody方法的典型用法代码示例。如果您正苦于以下问题:C# HttpMethod.GetBody方法的具体用法?C# HttpMethod.GetBody怎么用?C# HttpMethod.GetBody使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpMethod
的用法示例。
在下文中一共展示了HttpMethod.GetBody方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Execute
public HttpResponse Execute(HttpMethod httpMethod)
{
var request = (HttpWebRequest) WebRequest.Create(httpMethod.Uri);
var body = httpMethod.GetBody();
request.Timeout = 5000;
request.ReadWriteTimeout = 20000;
request.Method = httpMethod.Method;
request.ContentLength = body.ContentLength;
httpMethod.Headers.Keys.ForEach(name => request.Headers.Add(name, httpMethod.Headers[name]));
if (body.HasContent())
{
request.ContentType = body.ContentType;
using (var requestStream = request.GetRequestStream())
{
requestStream.Write(body.Data, 0, body.ContentLength);
}
}
try
{
var response = (HttpWebResponse)request.GetResponse();
return new HttpResponseFactory().Create(response);
}
catch (WebException e)
{
if (e.Response == null)
{
throw;
}
var response = (HttpWebResponse)e.Response;
return new HttpResponseFactory().Create(response);
}
}