Skip to main content
 首页 » 编程设计

c#之是否可以解决 .Net 3.5 中的 "Default parameter specifiers are not permitted"错误

2024年08月12日50Free-Thinker

我正在尝试为游戏设计一个插件,而该软件是基于 .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; 
        } 
    } 
} 

请您参考如下方法:

So I was wondering if it was possible to work around this in any way?

是的,重载:

public bool LoginToLee(string user, string pass) 
{ 
    return LoginToLee(user, pass, false); 
} 
 
public bool LoginToLee(string user, string pass, bool remember) 
{ 
    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. "); 
} 

此外,您不应该通过 HTTP 以明文形式发送密码。