本文整理汇总了C#中System.Net.HttpWebRequest.AddHeaders方法的典型用法代码示例。如果您正苦于以下问题:C# HttpWebRequest.AddHeaders方法的具体用法?C# HttpWebRequest.AddHeaders怎么用?C# HttpWebRequest.AddHeaders使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.HttpWebRequest
的用法示例。
在下文中一共展示了HttpWebRequest.AddHeaders方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Send_OTHERS
static public async Task<Response> Send_OTHERS(this Request request, Action<long[]> onProgress = null)
{
Response response = null;
try
{
#if NET45
var req= (HttpWebRequest)WebRequest.Create(request.Uri);
#else
var req= new HttpWebRequest(request.Uri);
#endif
req.Method = request.Method;
req.AddHeaders(request.Headers);
if (request.Content != null && request.Content.Length > 0)
{
var reqStream = Task.Factory.FromAsync<Stream>(req.BeginGetRequestStream, req.EndGetRequestStream, null).Result;
request.Content.CopyTo(reqStream);
reqStream.Flush();
}
//var res = (HttpWebResponse) await Task.Factory.FromAsync<WebResponse>(req.BeginGetResponse, req.EndGetResponse, null);
// weird problems with some datasoures, replace the above line for the following; this should not block UI cause the public interfaces
// are async
var wr = Task.Factory.FromAsync<WebResponse>(req.BeginGetResponse, req.EndGetResponse, null).Result;
var res = (HttpWebResponse) wr;
var resStream = default(Stream);
if (res.ContentLength > 0)
{
resStream = res.GetResponseStream();
}
response = await Response.Create(res.StatusCode, resStream);
foreach (var name in res.Headers.AllKeys)
{
response.Headers.Add(name, res.Headers[name]);
}
return response;
}
catch (Exception e)
{
throw e;
}
}