Andrew Andrew - 1 year ago 94
C# Question

ArgumentOutOfRangeException was unhandled

On line 69 I am getting the error "ArgumentOutOfRangeException was unhandled".

Please critique as I'm always looking for ways to improve my coding.

Usage Example:
MapleWebStart mws = new MapleWebStart("myusername", "mypassword");

using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using Microsoft.Win32;

public class MapleWebStart
private CookieContainer LoginCookie;

public MapleWebStart(string username, string password)
LoginCookie = new CookieContainer();
if (!Login(username, password))
throw new ArgumentException("Invalid Nexon username or password.");

public MapleWebStart(CookieContainer loginCookie)
LoginCookie = loginCookie;

private bool Login(string username, string password)
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("");
request.Method = "POST";
string postData = "__VIEWSTATE=%2FwEPDwULLTE5NjM3MjM3MDcPZBYCAgMPZBYEAgUPFgIeB1Zpc2libGVoZAIHDxYCHwBoZGSVGoua1TAiCeQWNk%2BdqOF%2FXtq%2BmA%3D%3D&tbID=" + username + "&" + "tbPass=" + password;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.CookieContainer = LoginCookie;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
if (!responseFromServer.Contains("Wrong password"))
return true;
return false;

private string GetMaplePath()
string maplePath = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Wizet\MapleStory", "ExecPath", "");
return File.Exists(maplePath) ? maplePath : (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Wizet\MapleStory", "ExecPath", "");

public void Start()
ProcessStartInfo maplestory = new ProcessStartInfo();
maplestory.FileName = GetMaplePath() + @"\MapleStory.exe";
maplestory.Arguments = "WebStart " + LoginCookie.GetCookies(new Uri(""))[0].ToString().Substring(4);

original code:

Answer Source

Your problem is that

LoginCookie.GetCookies(new Uri(""))[0].ToString()

returns a string with less than 5 characters. So, doing

LoginCookie.GetCookies(new Uri(""))[0].ToString().Substring(4)

will fail, since there will be no character at position 4. You have to check the string length before dealing with it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download