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

时间:2016-04-07 15:48:25

标签: c# libcurl libcurl.net

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

    static void Main(string[] args)
    {
        Curl.GlobalInit(CurlInitFlag.All);
        using (var easy = new CurlEasy())
        {
            easy.Url = "http://stackoverflow.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内容?

1 个答案:

答案 0 :(得分:0)

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

    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;
    }