Вопрос

I'm using the Http type of FSharpData.

type Http private() = 
    ......
    static member Request(url, ?query, ?headers, ?meth, ?body, ?cookies, ?cookieContainer, ?certificate) =
    ......

However, I need to extend static memeber Request to handle cookies. I cannot inherit the class since its constructor is private.

type Web () =
    inherit Http() // Error
    override Request
        let ObSSOCookie = new Cookie()
        ......

Or I had to create a module with functions that call the static member functions? Can the module has the same name of Http?

Это было полезно?

Решение

The Http type is a static class, because it's stateless, so it doesn't make sense to inherit from it. If you want to use cookies and maintain them across requests, you can just create a CookieContainer and reuse it across requests:

let cc = CookieContainer()
Http.RequestString("url1", cookieContainer = cc)
Http.RequestString("url2", cookieContainer = cc)

If you really want to, you can create a wrapper for it like this:

type HttpWithState() = 
    let cookies = CookieContainer()

    static member RequestString(url, ?params, ?headers, ...) =
        Http.RequestString(url, ?params = params, ?headers = headers, ..., cookieContainer = cookies)

and then use it like this:

let http = HttpWithState()
http.RequestString("url1")
http.RequestString("url2")

But you won't gain that much over using the Http type directly

Другие советы

I don't believe that you can inherit from this class - this is probably a design desicision.

However, the full path to that type is

FSharp.Net.Http

so you could create your own type in something like

A.B.Http

which could call the original http functions and provide overrides through that method.

You can try adding static extension method to Http class, if it doesn't need to access privates:

type FSharp.Net.Http with

    static member RequestWithCookies(...) = ...
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top