我想登錄到我和我的控制檯應用程序MyBB的論壇,但我得到一個錯誤與我的代碼爲「POSTDATA默認參數值必須是一個編譯時常
默認參數值'必須是編譯時常量
如果我將用戶名和密碼設置爲常量字符串,但我無法使用Console.ReadLine();所以我將不得不硬編碼的用戶名和密碼,我不認爲這是一個好主意。
這是我的代碼:
public string Username = Console.ReadLine();
public string Password = Console.ReadLine();
public const string ForumUrl = "forum.smurfbot.net";
static void Main(string[] args)
{
}
public string MakePostRequest(string url = "www.website.com/usercp.php", string postData = "username=" + Username + "&password=" + Password + "&remember=yes&submit=Login&action=do_login&url=" + ForumUrl + "member.php?action=login")
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.KeepAlive = true;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.AllowAutoRedirect = true;
byte[] postBytes = Encoding.ASCII.GetBytes(postData);
request.ContentLength = postBytes.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string sReturn = sr.ReadToEnd();
sr.Dispose();
return sReturn;
}