【问题标题】:How to append HTML content into a string in CurlSharp?如何将 HTML 内容附加到 CurlSharp 中的字符串中?
【发布时间】:2016-04-07 15:48:24
【问题描述】:

我正在使用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 内容?

【问题讨论】:

    标签: c# libcurl libcurl.net


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 2011-11-11
      • 1970-01-01
      相关资源
      最近更新 更多