2011-07-25 49 views
4

我试图连接到HTTPS页面。在C#中我可以写,要求C#与线如何在使用F#连接时接受SSL证书?

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; 

如何适应这F#轻松连接到HTTPS一条线吗?

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() 

回答

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(下划线)替代每个参数,这是一个习惯用语指示参数的ic方式未被使用。

+0

你可以评论你如何做到这一点? – unj2

+0

@ kunjan:好的。我添加的解释是否足够了? – Daniel

相关问题