CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2013
    Posts
    2

    How i can get html-code of twitter with curl library (c#))?

    I've a code:
    Code:
    Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
                Easy easy = new Easy();
                easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER,0);
                easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST,0);
                easy.SetOpt(CURLoption.CURLOPT_URL,"http://twitter.com");
                easy.SetOpt(CURLoption.CURLOPT_REFERER,"http://twitter.com");
                easy.SetOpt(CURLoption.CURLOPT_USERAGENT, "Mozilla/4.0 (Windows; U; Windows NT 5.0; En; rv:1.8.0.2) Gecko/20070306 Firefox/1.0.0.4");
    How i can get html-source of twitter with C# curl?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How i can get html-code of twitter with curl library (c#))?

    I'm not sure, but why jump to an external library when you can do what you want in C#?

    See the first link in the bing search: "getting the source of a web site in C#".

    ...
    Code:
    using System.Net;
    //...
    using (WebClient client = new WebClient ()) // WebClient class inherits IDisposable
    {
        client.DownloadFile("http://yoursite.com/page.html", @"C:\localfile.html");
    
        // Or you can get the file content without saving it:
        var htmlCode = client.DownloadString("http://yoursite.com/page.html");
        //...
    }

  3. #3
    Join Date
    Dec 2013
    Posts
    2

    Re: How i can get html-code of twitter with curl library (c#))?

    Quote Originally Posted by Arjay View Post
    I'm not sure, but why jump to an external library when you can do what you want in C#?
    Because i need do authorization on popular website.

    Now, i've a solution:
    http.cs file (class wroking with LibCurlNel.dll (curl library) ):
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using SeasideResearch.LibCurlNet;
    using System.IO;
    
    namespace WindowsFormsApplication1
    {
        class HTTP
        {
            private static Easy easy;
            private static Random rand = new Random();
            private static string SockBuff;
            private static string CookieFile = AppDomain.CurrentDomain.BaseDirectory + "cookie" + rand.Next(0, 9) + rand.Next(0, 9) + rand.Next(0, 9) + rand.Next(0, 9) + rand.Next(0, 9) + rand.Next(0, 9) + rand.Next(0, 9) + ".txt";
            public static string UserAgent = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko";
            public static string Proxy = "";
            public string referer = "";
    
            public void Dispose()
            {
                ClearCookies();
            }
            public string getCookieFile()
            {
                return CookieFile;
            }
    
            public void CurlInit()
            {
                Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
            }
    
            public void ClearCookies()
            {
                if (File.Exists(CookieFile))
                {
                    File.Delete(CookieFile);
                }
    
            }
    
            public string HTTPGet(string URL, string Proxy)
            {
                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 string HTTPPost(string URL, string http_headers,string Content)
            {
                easy = new Easy();
                SockBuff = "";
                String proxy;
                         
                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_HTTPHEADER, http_headers); 
                    easy.SetOpt(CURLoption.CURLOPT_REFERER, referer);
                    //easy.SetOpt(CURLoption.CURLOPT_POSTFIELDSIZE, Content.Length); 
                    easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, true);
                    easy.SetOpt(CURLoption.CURLOPT_STDERR, "test.txt");
    
                    easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, 1); 
    
                    easy.SetOpt(CURLoption.CURLOPT_POST, true);
                    easy.SetOpt(CURLoption.CURLOPT_POSTFIELDS, Content);
    
    
                    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
                {
    
                }
                return SockBuff;
    
            }
    
            public string SafeString(string data)
            {
                return Curl.Escape(data, data.Length);
            }
    
            public string UnSafeString(string data)
            {
                return Curl.Unescape(data, data.Length);
            }
    
            public static Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
            {
                // Console.Write(System.Text.Encoding.UTF8.GetString(  buf)); 
                SockBuff = SockBuff + System.Text.Encoding.UTF8.GetString(buf);
    
                return size * nmemb;
            }
    
        }
    }
    Your Form1 file:
    Code:
    HTTP httpVar = new HTTP();
    httpVar.CurlInit(); // cURL init
    httpVar.ClearCookies();
    
                String
                http_headers = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
                http_headers = http_headers + "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36";
                http_headers = http_headers + "Accept-Encoding: gzip,deflate,sdch";
                http_headers = http_headers + "Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4";
               
    
                string page = httpVar.HTTPPost("https://twitter.com/login",http_headers,""); // URL and proxy
                webBrowser1.DocumentText = page;
                textBox1.Text = System.IO.File.ReadAllText(@httpVar.getCookieFile());

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured