Libcurl通过线程

时间:2010-09-10 14:25:52

标签: c# libcurl

我在c#上编码程序(C#,& object oriented programming newbie) 我试图在多个线程中下载页面。仅运行1个线程时没有问题。运行多个时出现问题。

似乎所有线程都将下载的信息保存在同一个变量SockBuff中,但我不知道如何解决这个问题。

建议?

这是代码

单击按钮时程序启动。

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        void TestaThread_1()
        {
            Random RandomNumber = new Random();
            int rand = RandomNumber.Next(99);

            HTTP cURL = new HTTP();
            cURL.CurlInit();
            String data = cURL.HTTPGet("http://google.com", "", "fails" + rand.ToString() + ".html");
            HTTP.save_file("fails" + rand.ToString()+ ".html", data);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread thread1 = new Thread(new ThreadStart(TestaThread_1));
            thread1.Start();

            Thread thread2 = new Thread(new ThreadStart(TestaThread_1));
            thread2.Start();

            Thread thread3 = new Thread(new ThreadStart(TestaThread_1));
            thread3.Start();

            Thread thread4 = new Thread(new ThreadStart(TestaThread_1));
            thread4.Start();

            Thread thread5 = new Thread(new ThreadStart(TestaThread_1));
            thread5.Start();
        }
    }

    class HTTP
    {
        public Easy easy;        
        public static string SockBuff;
        public string CookieFile = AppDomain.CurrentDomain.BaseDirectory + "cookie.txt";
        public string UserAgent = "Mozilla 5.0";
        public string Proxy = "";


        public void CurlInit()
        {
            Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
        }


        public string HTTPGet(string URL, string Proxy, String FailaNosaukums)
        {
            easy = new Easy();
            SockBuff = "";

            try
            {
                Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);

                easy.SetOpt(CURLoption.CURLOPT_URL, URL);
                easy.SetOpt(CURLoption.CURLOPT_TIMEOUT, "60");
                easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
                easy.SetOpt(CURLoption.CURLOPT_USERAGENT, UserAgent);
                easy.SetOpt(CURLoption.CURLOPT_COOKIEFILE, CookieFile);
                easy.SetOpt(CURLoption.CURLOPT_COOKIEJAR, CookieFile);
                easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, true);

                if (URL.Contains("https"))
                {
                    easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, 1);
                    easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, 0);
                }

                if (Proxy != "")
                {
                    easy.SetOpt(CURLoption.CURLOPT_PROXY, Proxy);
                    easy.SetOpt(CURLoption.CURLOPT_PROXYTYPE, CURLproxyType.CURLPROXY_HTTP);
                }

                easy.Perform();
                easy.Cleanup();

            }
            catch
            {
                Console.WriteLine("Get Request Error");
            }

            return SockBuff;
        }


        public static Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
        {
            Random rand = new Random();
            int RandomNumber = rand.Next(1000);

            SockBuff = SockBuff + System.Text.Encoding.UTF8.GetString(buf);

            return size * nmemb;
        }

        static public void save_file(string file_name, string text_to_write)
        {
            StreamWriter MyStream = null;
            string MyString = text_to_write;

            try
            {
                MyStream = File.CreateText(file_name);
                MyStream.Write(MyString);
            }
            catch (IOException e)
            {
                Console.WriteLine(e);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                if (MyStream != null)
                    MyStream.Close();
            }
        }
    }
}

我还有另一个问题。

为什么我无法正确使用save_file函数保存图像文件? buff中是否存在编码问题?

1 个答案:

答案 0 :(得分:1)

问题是您的SockBuffstatic,因此HTTP类的所有实例只共享该变量的一个实例。您可以通过删除static来解决问题。

如果你这样做,你还必须使你的OnWriteData方法成为实例方法(即不是静态的)。

您也可以考虑使用System.Net.WebClient和/或System.Net.HttpWebRequest类而不是Libcurl。