如何将 HTML 内容附加到 CurlSharp 中的字符串中?

How to append HTML content into a string in CurlSharp?

我正在使用 CurlSharp 库来获取 HTTP。这是我目前得到的代码:

    static void Main(string[] args)
    {
        Curl.GlobalInit(CurlInitFlag.All);
        using (var easy = new CurlEasy())
        {
            easy.Url = "http://whosebug.com/";
            easy.FollowLocation = true;      
            easy.WriteData = null;
            easy.WriteFunction = OnWriteData;

            easy.Perform();
        }
    }

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

但是OnWriteData()被调用了很多次。是否可以在字符串中附加 buf 以获得所有 HTML 内容?

我通过这样做找到了解决方案:

    public static string sContent;

    public static Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
    {
        string encoding = "UTF-8";
        sContent += System.Text.Encoding.GetEncoding(encoding).GetString(buf);
        return size * nmemb;
    }