Use LibCurl .net to download a file [closed]

一个人想着一个人 提交于 2020-01-04 14:18:08

问题


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!


回答1:


There appears to be a whole samples section in the source code 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;
    }
}


来源:https://stackoverflow.com/questions/24567344/use-libcurl-net-to-download-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!