【发布时间】:2014-11-23 04:38:32
【问题描述】:
我正在尝试为游戏设计一个插件,该软件是基于 .Net 3.5 构建的。
我要实现的代码是:
public bool LoginToLee(string user, string pass, bool remember = false)
{
string res = Net.GetResponse("http://leeizazombie.com/member.php?action=login", new Dictionary<string, string>() { { "username", username }, { "password", password }, { "remember", (remember ? "yes" : "no") }, { "action", "do_login" }, { "url", "" } });
return !res.Contains("You have entered an invalid username/password combination. ");
}
我得到的错误是“不允许使用默认参数说明符”
我很清楚,如果我使用 .Net 4.0 编译插件,则不会发生此错误,但不幸的是,我无法在 .Net 3.5 程序上支持该插件。
所以我想知道是否有可能以任何方式解决这个问题?
哦,顺便说一句,如果有必要查看“Net.GetResponse”的一部分,代码如下:
namespace NetUtils
{
public class Net
{
public static bool IsUrl(object obj) { try { new Uri(obj.ToString()); return true; } catch { return false; } }
private static string Data2Post(Dictionary<string, string> data)
{
string postData = "";
foreach (KeyValuePair<string, string> k in data)
{
if (postData != "") { postData += '&'; }
postData = postData + k.Key + "=" + k.Value;
}
return postData;
}
public static string GetResponse(string url, byte[] data)
{
if (!IsUrl(url))
{
return "Invalid url.";
}
return GetResponseInternal(new Uri(url), data);
}
public static string GetResponse(string url, Dictionary<string, string> data)
{
if (!IsUrl(url))
{
return "Invalid url.";
}
return GetResponse(new Uri(url), Data2Post(data));
}
public static string GetResponse(string url, string data)
{
if (!IsUrl(url))
{
return "Invalid url.";
}
return GetResponse(new Uri(url), data);
}
public static string GetResponse(Uri url, Dictionary<string, string> data)
{
return GetResponse(url, Data2Post(data));
}
public static string GetResponse(Uri url, string data)
{
return GetResponseInternal(url, new UTF8Encoding().GetBytes(data));
}
public static string GetResponse(Uri url, byte[] data)
{
return GetResponseInternal(url, data);
}
private static string GetResponseInternal(Uri url, byte[] data)
{
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(url);
httpWReq.Method = "POST";
httpWReq.ContentType = "application/x-www-form-urlencoded";
httpWReq.ContentLength = data.Length;
using (Stream stream = httpWReq.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
return responseString;
}
}
}
【问题讨论】:
标签: c#