var Testrequest = await new HttpRequest( new HttpRequestConfig { Url = new Uri("http://httpbin.org/headers"), UserAgentHeader = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0", AwaitProcessing = AwaitProcessingEnum.All }); Console.WriteLine(Testrequest.Response.Json.ToString());
I obtain this result. { "headers": { "Accept-Charset": "utf-8,ISO-8859-1;q=0.8,*;q=0.5", "Accept-Encoding": "gzip,deflate", "Host": "httpbin.org", "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0" } }
Rem : Tom, Why don't create a Headers property with an initialize list? So we could add elements like the User Agent eg : Headers.Add(new KeyValuePair<string, string>("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0")
You can Add() to the headers as you described, you have to initialize the list first:
var config = new HttpRequestConfig { Headers = new List<KeyValuePair<string, string>>() }; config.Headers.Add(new KeyValuePair<string, string>("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0"); ... var request = await new HttpRequest(config);
This avoid the allocation of a empty Headers list when none is needed in the specific context.
P.S. The style sheet for the code sections in this forms are crap! We must fix this.