Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Impossible to inherit from HttpWebRequest?

Impossible to inherit from HttpWebRequest?

Scheduled Pinned Locked Moved .NET (Core and Framework)
question
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Steven Campbell
    wrote on last edited by
    #1

    Yesterday I was trying to inherit from HttpWebRequest, to implement a protocol that extends HTTP. In the end, I think it is impossible...although the class itself can be inherited from, its main constructor is effectively private (probably declared "internal"), which means that my derived class cannot construct the base class. This is due to the nature of the HttpWebRequest, which is always created through the WebRequest.Create() method, never directly through its constructor. Does anyone have any experience with or insight into this?

    M 1 Reply Last reply
    0
    • S Steven Campbell

      Yesterday I was trying to inherit from HttpWebRequest, to implement a protocol that extends HTTP. In the end, I think it is impossible...although the class itself can be inherited from, its main constructor is effectively private (probably declared "internal"), which means that my derived class cannot construct the base class. This is due to the nature of the HttpWebRequest, which is always created through the WebRequest.Create() method, never directly through its constructor. Does anyone have any experience with or insight into this?

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #2

      How do you want to extend HTTP? If you want to add headers, you can add them to the Headers collection. If you want to use a custom verb, you can do that too: set the Method property. If you want to customise these properties in a fixed way, and still plug in to WebRequest.Create, I think you'll have to derive a new class from WebRequest, have an HttpWebRequest as a member, and forward methods to your HttpWebRequest. Stability. What an interesting concept. -- Chris Maunder

      S 1 Reply Last reply
      0
      • M Mike Dimmick

        How do you want to extend HTTP? If you want to add headers, you can add them to the Headers collection. If you want to use a custom verb, you can do that too: set the Method property. If you want to customise these properties in a fixed way, and still plug in to WebRequest.Create, I think you'll have to derive a new class from WebRequest, have an HttpWebRequest as a member, and forward methods to your HttpWebRequest. Stability. What an interesting concept. -- Chris Maunder

        S Offline
        S Offline
        Steven Campbell
        wrote on last edited by
        #3

        You are right, I can add to the headers, and set the Method property. That is what I ended up doing. And it is a simpler design this way, so everything worked out for the best (so far at least). However, I found it strange that MS would create a non-sealed class that could not be inherited. A sealed class is faster after all...and I would think that they would want the framework to be as efficient as possible. So I figured it may just be that there is some technique that I am unaware of - I'm still a C# newbie in many ways :((. I'd still like to be able to inherit from it if the design evolves that way. Decorating objects (your 2nd suggestion) is usually a last resort for me, as it is always so repetitive. [Since you asked, I am writing a 100% managed C# component for working with the client side of WebDAV. (Yes, I know that I can use MSXML). WebDAV is an extension to HTML, so it shares all the same basic characteristics. Extension equates well to inheritance, so that's why my first thought was to inherit from HttpWebRequest.]

        A 1 Reply Last reply
        0
        • S Steven Campbell

          You are right, I can add to the headers, and set the Method property. That is what I ended up doing. And it is a simpler design this way, so everything worked out for the best (so far at least). However, I found it strange that MS would create a non-sealed class that could not be inherited. A sealed class is faster after all...and I would think that they would want the framework to be as efficient as possible. So I figured it may just be that there is some technique that I am unaware of - I'm still a C# newbie in many ways :((. I'd still like to be able to inherit from it if the design evolves that way. Decorating objects (your 2nd suggestion) is usually a last resort for me, as it is always so repetitive. [Since you asked, I am writing a 100% managed C# component for working with the client side of WebDAV. (Yes, I know that I can use MSXML). WebDAV is an extension to HTML, so it shares all the same basic characteristics. Extension equates well to inheritance, so that's why my first thought was to inherit from HttpWebRequest.]

          A Offline
          A Offline
          Abel Braaksma
          wrote on last edited by
          #4

          The reason it cannot be inherited is because HttpWebRequest does not have a public default constructor that takes zero arguments. If you use code like this (note: not safe, you must provide proper arguments for base(..,..)) it will compile and work:

          public class SafeHttpWebRequest : HttpWebRequest
          {
          public SafeHttpWebRequest() : base(null, new StreamingContext()) { /* do something */ }
          }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups