使用LibCurl .net下载文件 [英] Use LibCurl .net to download a file

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

问题描述

如何简单地使用libCurl .net下载文件?我无法使用WebClient。我试图寻找一个很好的例子,但找不到任何东西。谢谢!

How do I simply download a file with libCurl .net? I can't use WebClient. I tried looking for a good example but cant find anything. Thanks!

推荐答案

在源代码 http://libcurl-net.cvs.sourceforge.net/viewvc/libcurl-net/libcurlnet/samples/

我认为与您最相关的一个是轻松获取

I think the one that is most relevant to you is Easy Get

// $Id: EasyGet.cs,v 1.1 2005/02/17 22:47:24 jeffreyphillips Exp $
// EasyGet.cs - demonstrate trivial get capability
// Compile with "csc /r:../bin/LibCurlNet.dll /out:../bin/EasyGet.exe EasyGet.cs"

// usage: EasyGet url, e.g. EasyGet http://www.google.com

using System;
using SeasideResearch.LibCurlNet;

class EasyGet
{
    public static void Main(String[] args)
    {
        try {
            Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);

            Easy easy = new Easy();
            Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);

            easy.SetOpt(CURLoption.CURLOPT_URL, args[0]);
            easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
            easy.Perform();
            easy.Cleanup();

            Curl.GlobalCleanup();
        }
        catch(Exception ex) {
            Console.WriteLine(ex);
        }
    }

    public static Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb,
        Object extraData)
    {
        Console.Write(System.Text.Encoding.UTF8.GetString(buf));
        return size * nmemb;
    }
}

这篇关于使用LibCurl .net下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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