Domanda

I'm creating a program to download web pages using the Http module of FSharp.Data. However, the module doesn't support setting http proxy server. In C# there is

_httpWebRequest.Proxy = 
    new System.Net.WebProxy("http://proxy.myCompany.com:80", true);

I tried to download the file from https://github.com/fsharp/FSharp.Data/blob/master/src/Library/Http.fs and use it directly in my F# project. However, the type of response changed from string to HttpResponse after I call the Http.Request from the downloaded file.

let response = 
    Http.Request (
        url, 
        query=["userid", user; "password", password; "login", "Sign+On"], 
        meth="POST", 
        cookieContainer = cc)

What's the best way to extend the Http module with proxy support?

È stato utile?

Soluzione

In FSharp.Data 2.0, you can pass the parameter customizeHttpRequest of type HttpWebRequest->HttpWebRequest to set the proxy like this:

Http.Request (
    url, 
    query=["userid", user; "password", password; "login", "Sign+On"], 
    meth="POST", 
    cookieContainer = cc,
    customizeHttpRequest = (fun req -> req.Proxy <- WebProxy("http://proxy.myCompany.com:80", true); req))

Altri suggerimenti

In the new version (upcoming release), we are renaming the current Http.Request to Http.RequestString and the current Http.RequestDetailed to Http.Request. This is a breaking change, but we thing it makes much more sense (and fits better with standard .NET naming). If you just want to copy the old file, you can always get the older version of the code from the appropriate branch on GitHub (e.g. Http.fs @ tag v1.1.10).

However, I think that supporting HTTP proxies would be a great addition to the library. So the best thing to do would be to fork the project to your GitHub, add the feature and submit a pull request! I think that just adding an optional ?proxy parameter to the two methods and propagating the information to the underlying HttpWebRequestwould be the best way to do this.

The only tricky thing is that Http.Request should work on multiple versions of .NET (including Windows Phone, Silverlight, etc.) so you may need to check which of them actually support specifying the proxy.

If you do not have the time for helping out, then please submit a GitHub issue.

Have you tried overriding the proxy globally with WebRequest.DefaultWebProxy = new System.Net.WebProxy("http://proxy.myCompany.com:80", true)?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top