我想登录到我和我的控制台应用程序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;
}