C# 从网上下载文件

发布于 2024-10-31 00:38:46 字数 218 浏览 6 评论 0原文

有没有办法让以下功能通过代理工作?

public T[] ReadStream(System.IO.TextReader reader);

我希望能够代理 reader 实例,以便它可以从网络读取尝试并将其缓存在某处。

或者也许为此目的有一些默认设置?

Is there a way to make the following function work via proxy?

public T[] ReadStream(System.IO.TextReader reader);

I want to be able to proxify the reader instance, so that it could download a file from the web on reading attempts and cache it somewhere.

Or maybe there is something default for this purpose?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

单调的奢华 2024-11-07 00:38:46

使用 WebClient.DownloadFile。如果需要代理,可以设置 WebClient 对象的 Proxy 属性。

这是一个示例:

using (var client = new WebClient())
{
    client.Proxy = new WebProxy("some.proxy.com", 8000);
    client.DownloadFile("example.com/file.jpg", "file.jpg");
}

您还可以使用 BinaryReader 分段下载文件:

using (var client = new WebClient())
{
    client.Proxy = new WebProxy("some.proxy.com", 8000);

    using (var reader = new BinaryReader(client.OpenRead("example.com/file.jpg")))
    {
        reader.ReadByte();
        reader.ReadInt32();
        reader.ReadBoolean();

        // etc.
    }
}

Use WebClient.DownloadFile. If you need a proxy, you can set the Proxy property of your WebClient object.

Here's an example:

using (var client = new WebClient())
{
    client.Proxy = new WebProxy("some.proxy.com", 8000);
    client.DownloadFile("example.com/file.jpg", "file.jpg");
}

You can also download the file by parts with a BinaryReader:

using (var client = new WebClient())
{
    client.Proxy = new WebProxy("some.proxy.com", 8000);

    using (var reader = new BinaryReader(client.OpenRead("example.com/file.jpg")))
    {
        reader.ReadByte();
        reader.ReadInt32();
        reader.ReadBoolean();

        // etc.
    }
}
客…行舟 2024-11-07 00:38:46

也许这就是你想要的?鉴于您对前一个答案的评论,我对问题的措辞也有点困惑。

public StreamReader GetWebReader(string uri)
{
    var webRequest = WebRequest.Create(uri);
    var webResponse = webRequest.GetResponse();
    var responseStream = webResponse.GetResponseStream();
    return new StreamReader(responseStream);
}

Perhaps this is what you want? I am also slightly confused by the wording of the question, given your comments to the previous answer.

public StreamReader GetWebReader(string uri)
{
    var webRequest = WebRequest.Create(uri);
    var webResponse = webRequest.GetResponse();
    var responseStream = webResponse.GetResponseStream();
    return new StreamReader(responseStream);
}
因为看清所以看轻 2024-11-07 00:38:46
var webRequestObject = (HttpWebRequest) WebRequest.Create("http://whatever");
var response = webRequestObject.GetResponse();
var webStream = response.GetResponseStream();
// Ta-da.
var reader = new StreamReader(webStream);
var webRequestObject = (HttpWebRequest) WebRequest.Create("http://whatever");
var response = webRequestObject.GetResponse();
var webStream = response.GetResponseStream();
// Ta-da.
var reader = new StreamReader(webStream);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文