HttpWebRequest Extension

class AdvancedWebRequest : HttpWebRequest { private static readonly ILog log = log4net.LogManager.GetLogger(typeof(AdvancedWebRequest)); public AdvancedWebRequest(string url, CookieContainer cookies = null) { Create(url); UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22"; Referer = Address.AbsoluteUri; if (cookies == null) { CookieContainer = Program.request.CookieContainer; } else { CookieContainer = cookies; } } } 

This is what I want to do, so basically getting an HttpWebRequest with some variables already set, so I should not always set them myself.

Getting error:

 'System.Net.HttpWebRequest.HttpWebRequest()' is obsolete: 'This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.' \Extensions\AdvancedWebRequest.cs 14 10 

Any suggestions cannot really be extended by HttpWebRequest ?

+4
source share
2 answers

You cannot inherit from HttpWebRequest , but you can inherit from WebRequest

Check out the answers to a similar question here .

+3
source

You can use extension methods, for example:

 public static DoRequest(this HttpWebRequest req){ //do something } 

and then you can call this method as follows:

 webrequest.DoRequest(); 

For more information, check out the MSDN page: http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx

+2
source

All Articles