Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending HttpWebRequest

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 something I want to do, so basically get the HttpWebRequest with some variables already set, so I must not set them always 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, can't HttpWebRequest be really extended?

like image 475
Jaanus Avatar asked Mar 02 '13 12:03

Jaanus


People also ask

What is the difference between HttpWebRequest and WebRequest?

In a nutshell, WebRequest—in its HTTP-specific implementation, HttpWebRequest—represents the original way to consume HTTP requests in . NET Framework. WebClient provides a simple but limited wrapper around HttpWebRequest. And HttpClient is the new and improved way of doing HTTP requests and posts, having arrived with .

What is the default timeout for HttpWebRequest?

The default value is 100,000 milliseconds (100 seconds).

What is HttpWebRequest?

The HttpWebRequest class provides support for the properties and methods defined in WebRequest and for additional properties and methods that enable the user to interact directly with servers using HTTP.

Is HttpWebRequest disposable?

from a quick check in the object browser HttpWebRequest isn't seem to be disposable. it needs to have IDisposable interface.


2 Answers

You can not inherit fromHttpWebRequest but you can inherit from WebRequest

Check the answers from the similar question here.

like image 107
Davor Zlotrg Avatar answered Oct 17 '22 19:10

Davor Zlotrg


You could use Extension Methods like this:

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

and then you can call this method like this:

webrequest.DoRequest();

For more infos, look at the MSDN Page: http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx

like image 24
Tearsdontfalls Avatar answered Oct 17 '22 19:10

Tearsdontfalls