【发布时间】:2015-10-07 08:20:02
【问题描述】:
我想使用控制台应用程序登录 MyBB 论坛,但我的代码出现错误
'postData' 的默认参数值必须是编译时常量
如果我将我的用户名和密码设置为 const 字符串,我可以很容易地修复它,但我不能使用 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;
}
【问题讨论】: