使用F#连接时如何接受SSL证书?

时间:2011-07-25 19:09:47

标签: http ssl f#

我尝试连接到HTTPS页面。在C#中,我可以写一行,要求C#使用行

轻松连接到HTTPS
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

我如何将其改编为f#?

open System.Net
open System.IO

let url = "https://..."

let mutable request = HttpWebRequest.Create(url)
request.Method <- "GET"  
request.ContentType <- "multipart/form-data"

ServicePointManager.ServerCertificateValidationCallback = fun -> true ???? 

let mutable resp = request.GetResponse()

let fn =
    for i = 1 to 10 do
        request <- WebRequest.Create(url)
        resp <- request.GetResponse()

1 个答案:

答案 0 :(得分:11)

ServicePointManager.ServerCertificateValidationCallback <-
  System.Net.Security.RemoteCertificateValidationCallback(fun _ _ _ _ -> true)

以下也适用:

System.Net.ServicePointManager.ServerCertificateValidationCallback <- 
  (fun _ _ _ _ -> true) //four underscores (and seven years ago?)

RemoteCertificateValidationCallback具有以下签名:

public delegate bool RemoteCertificateValidationCallback(
    Object sender,
    X509Certificate certificate,
    X509Chain chain,
    SslPolicyErrors sslPolicyErrors
)

由于函数参数发生模式匹配,并且您忽略了所有四个参数,因此可以用wildcard pattern(下划线)替换每个参数,这是表示参数未使用的惯用方法。