2012-08-08 98 views
0

在第69行,我收到錯誤「ArgumentOutOfRangeException was unhandled」。ArgumentOutOfRangeException未處理

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

Usage Example: 
MapleWebStart mws = new MapleWebStart("myusername", "mypassword"); 
mws.Start(); 
*/ 

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("http://maplestory.nexon.net/Controls/Passport.aspx"); 
     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); 
     dataStream.Close(); 
     HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
     dataStream = response.GetResponseStream(); 
     StreamReader reader = new StreamReader(dataStream); 
     string responseFromServer = reader.ReadToEnd(); 
     reader.Close(); 
     dataStream.Close(); 
     response.Close(); 
     if (!responseFromServer.Contains("Wrong password")) 
     { 
      LoginCookie.Add(response.Cookies); 
      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("http://nexon.net"))[0].ToString().Substring(4); 
     Process.Start(maplestory); 
    } 

原代碼:http://pastie.org/private/vgpxht8tehtdjml2jdq

+1

69行是一個空行。你的意思是第66行嗎?編輯:忘記這個評論,有一個線路號碼(當使用非默認主題時)pastie.org上的錯誤, – ken2k 2012-08-08 15:38:19

回答

1

您的問題是

LoginCookie.GetCookies(new Uri("http://nexon.net"))[0].ToString() 

返回少於5個字符的字符串。所以,做

LoginCookie.GetCookies(new Uri("http://nexon.net"))[0].ToString().Substring(4) 

將失敗,因爲在位置4將不會有字符。您必須在處理它之前檢查字符串的長度。