none
How to connect webclient with proxies RRS feed

  • Question

  • So in the past I've made 2 web scapers and they both have an issue I dont want to discuss about here.But I foundd out from competitors that I need to use proxies.So here is what I would like to do:User puts proxie list in special Proxies.txt file and for every interation it connects to mojang api with different proxy.

    Here is my code:

       class Program
        {
            static Regex idcheck = new Regex(@"id"":""(.+?)"",");
            static Regex Migrationcheck = new Regex(@"legacy"":(.+?)}");
    
            private static String _outputFileUnmigrated = "Unmigrated.txt";
            private static String _outputFileMigrated = "Migrated.txt";
    
            static void Main(string[] args)
            {
                var lines = File.ReadLines("List.txt");
                var UnmigratedNumber = 0;
                var MigratedNumber = 0;
                Console.Title = "Cookie Legacy checker | by BataBo | Checked: " + (UnmigratedNumber + MigratedNumber) + " | Good:" + UnmigratedNumber;
    
                using (StreamWriter UnmigratedWriter = File.AppendText(_outputFileUnmigrated))
                {
                    using (StreamWriter MigratedWriter = File.AppendText(_outputFileMigrated))
                    {
                        foreach (var line in lines)
                        {
                            args = new[]
                            {
                        "https://api.mojang.com/users/profiles/minecraft/" + line
                    };
    
                            using (var client1 = new WebClient())
                            {
                                var content = ScrubContent(client1.DownloadString(args[0]));
                                var idchecked = idcheck.Matches(content).Cast<Match>().Single().Groups[1];
    
                                args = new[]
                           {
                        "https://sessionserver.mojang.com/session/minecraft/profile/" + idchecked
    
                   };
                                using (var client2 = new WebClient())
                                {
                                    var content1 = ScrubContent(client2.DownloadString(args[0]));
                                    try { 
                                    var migrationchecked = Migrationcheck.Matches(content1).Cast<Match>().Single().Groups[1];
                                        Console.ForegroundColor = ConsoleColor.Green;
                                        Console.WriteLine("UNMIGRATED " + line);
                                        UnmigratedWriter.WriteLine(line);
                                        UnmigratedNumber++;
                                        Console.Title = "Cookie Legacy checker | by BataBo | Checked: " + (UnmigratedNumber + MigratedNumber) + " | Good:" + UnmigratedNumber;
                                    }
                                    catch(InvalidOperationException ex)
                                    {
                                        
                                        Console.ForegroundColor = ConsoleColor.Red;
                                        Console.WriteLine("MIGRATED " + line);
                                        MigratedWriter.WriteLine(line);
                                        MigratedNumber++;
                                        Console.Title = "Cookie Legacy checker | by BataBo | Checked: " + (UnmigratedNumber + MigratedNumber) + " | Good:" + UnmigratedNumber;
                                    }
                                   
    
    
                    
                                }
                            }
    
    
    
                        }
                    }
                }
                Console.WriteLine("");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("====================Results====================");
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("Checked: {0} names", UnmigratedNumber + MigratedNumber);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Unmigrated: {0} accounts", UnmigratedNumber);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Migrated: {0} accounts", MigratedNumber);
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("====================Results====================");
                Console.ReadLine();
            }
    
            static string ScrubContent(string content)
            {
                return new string(content.Where(c => c != '\n').ToArray());
            }
        }

    Sorry for asking so many qeustions I'm just not good at c#

    • Moved by May Luo-MSFT Thursday, January 10, 2019 5:48 AM c# issue
    Wednesday, January 9, 2019 9:05 PM

Answers

  • Hi BataBo Jokviu,

    >>So here is what I would like to do:User puts proxie list in special Proxies.txt file and for every interation it connects to mojang api with different proxy.

    We could use try/match and a loop method to achieve it. here is a sample for your reference.

    using (var client2 = new WebClient())
                                {
                                    var content1 = string.Empty;
                                    var porxies = File.ReadLines("Proxies.txt");
                                    foreach (var porxy in porxies)
                                    {
                                        WebProxy wp = new WebProxy(porxy.ToString());
                                        client2.Proxy = wp;
                                        try
                                        {
                                            content1 = ScrubContent(client2.DownloadString(args[0]));
                                            break;
                                        }
                                        catch (Exception ex)
                                        {
                                            Console.WriteLine("{0}", ex.Message);
                                        }
                                    }
                                    
                                    try
                                    {
                                        var migrationchecked = Migrationcheck.Matches(content1).Cast<Match>().Single().Groups[1];
                                        Console.ForegroundColor = ConsoleColor.Green;
                                        Console.WriteLine("UNMIGRATED " + line);
                                        UnmigratedWriter.WriteLine(line);
                                        UnmigratedNumber++;
                                        Console.Title = "Cookie Legacy checker | by BataBo | Checked: " + (UnmigratedNumber + MigratedNumber) + " | Good:" + UnmigratedNumber;
                                        break;
                                    }
                                    catch (InvalidOperationException ex)
                                    {
    
                                        Console.ForegroundColor = ConsoleColor.Red;
                                        Console.WriteLine("MIGRATED " + line);
                                        MigratedWriter.WriteLine(line);
                                        MigratedNumber++;
                                        Console.Title = "Cookie Legacy checker | by BataBo | Checked: " + (UnmigratedNumber + MigratedNumber) + " | Good:" + UnmigratedNumber;
                                    }
                                    
                                }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by BataBo Jokviu Wednesday, January 16, 2019 10:04 PM
    Wednesday, January 16, 2019 2:12 AM
    Moderator

All replies

  • Hi BataBo Jokviu,

    Welcome to the MSDN forum.

    Based on your code and description. It is related to C#. since our forum is mainly discussing with VS IDE issue. I will help you move it to the C# forum for a better supporting.

    Thank you for your cooperation.

    Br

    May


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Thursday, January 10, 2019 5:31 AM
  • Hi BataBo Jokviu,

    >>So here is what I would like to do:User puts proxie list in special Proxies.txt file and for every interation it connects to mojang api with different proxy.

    We could use try/match and a loop method to achieve it. here is a sample for your reference.

    using (var client2 = new WebClient())
                                {
                                    var content1 = string.Empty;
                                    var porxies = File.ReadLines("Proxies.txt");
                                    foreach (var porxy in porxies)
                                    {
                                        WebProxy wp = new WebProxy(porxy.ToString());
                                        client2.Proxy = wp;
                                        try
                                        {
                                            content1 = ScrubContent(client2.DownloadString(args[0]));
                                            break;
                                        }
                                        catch (Exception ex)
                                        {
                                            Console.WriteLine("{0}", ex.Message);
                                        }
                                    }
                                    
                                    try
                                    {
                                        var migrationchecked = Migrationcheck.Matches(content1).Cast<Match>().Single().Groups[1];
                                        Console.ForegroundColor = ConsoleColor.Green;
                                        Console.WriteLine("UNMIGRATED " + line);
                                        UnmigratedWriter.WriteLine(line);
                                        UnmigratedNumber++;
                                        Console.Title = "Cookie Legacy checker | by BataBo | Checked: " + (UnmigratedNumber + MigratedNumber) + " | Good:" + UnmigratedNumber;
                                        break;
                                    }
                                    catch (InvalidOperationException ex)
                                    {
    
                                        Console.ForegroundColor = ConsoleColor.Red;
                                        Console.WriteLine("MIGRATED " + line);
                                        MigratedWriter.WriteLine(line);
                                        MigratedNumber++;
                                        Console.Title = "Cookie Legacy checker | by BataBo | Checked: " + (UnmigratedNumber + MigratedNumber) + " | Good:" + UnmigratedNumber;
                                    }
                                    
                                }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by BataBo Jokviu Wednesday, January 16, 2019 10:04 PM
    Wednesday, January 16, 2019 2:12 AM
    Moderator