强制WebClient使用SSL [英] Force WebClient to use SSL

查看:256
本文介绍了强制WebClient使用SSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了这篇文章,给出了使用 WebClient 。这个很简单,但是如何强制它使用 SSL

I saw this post giving the simple idea of uploading files to ftp by using WebClient. This is simple, but how do I force it to use SSL?

推荐答案

Edward的在此处回答 Brey 可能会回答您的问题。除了提供自己的答案外,我只复制爱德华说的话:

The answer here by Edward Brey might answer your question. Instead of providing my own answer I will just copy what Edward says:

您可以使用FtpWebRequest;但是,这是相当低的水平。有一个较高级别的WebClient类,在许多情况下,所需的代码少得多。但是,默认情况下它不支持FTP / SSL。幸运的是,您可以通过注册自己的前缀使WebClient与FTP / SSL一起使用:

You can use FtpWebRequest; however, this is fairly low level. There is a higher-level class WebClient, which requires much less code for many scenarios; however, it doesn't support FTP/SSL by default. Fortunately, you can make WebClient work with FTP/SSL by registering your own prefix:

private void RegisterFtps()
{
    WebRequest.RegisterPrefix("ftps", new FtpsWebRequestCreator());
}

private sealed class FtpsWebRequestCreator : IWebRequestCreate
{
    public WebRequest Create(Uri uri)
    {
        FtpWebRequest webRequest = (FtpWebRequest)WebRequest.Create(uri.AbsoluteUri.Remove(3, 1)); // Removes the "s" in "ftps://".
        webRequest.EnableSsl = true;
        return webRequest;
    }
}

一旦执行此操作,就可以像使用WebRequest一样正常,除了您的URI以 ftps://而不是 ftp://开头。一个警告是您必须指定方法,因为将没有默认方法。例如,

Once you do this, you can use WebRequest almost like normal, except that your URIs start with "ftps://" instead of "ftp://". The one caveat is that you have to specify the method, since there won't be a default one. E.g.

// Note here that the second parameter can't be null.
webClient.UploadFileAsync(uploadUri, WebRequestMethods.Ftp.UploadFile, fileName, state);

这篇关于强制WebClient使用SSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆