【问题标题】:Is it possible to work around the "Default parameter specifiers are not permitted" error in .Net 3.5?是否可以解决 .Net 3.5 中的“不允许使用默认参数说明符”错误?
【发布时间】: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#


    【解决方案1】:

    所以我想知道是否有可能以任何方式解决这个问题?

    是的,做一个重载:

    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 以明文形式发送密码。

    【讨论】:

    • 谢谢,我试试,这只是验证身份验证
    猜你喜欢
    • 2011-12-10
    • 2014-09-28
    • 1970-01-01
    • 2014-12-10
    • 2011-09-17
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多