Exemplo n.º 1
0
    public static LitJson.JsonData postHttpWebRequest(string url, string method, string param)
    {
        // 转换输入参数的编码类型,获取byte[]数组
        byte[] byteArray = BinaryUtility.stringToBytes("gamedata=" + param, Encoding.UTF8);
        // 初始化新的webRequst
        // 1. 创建httpWebRequest对象
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url + "/" + method));

        // 2. 初始化HttpWebRequest对象
        webRequest.Method        = "POST";
        webRequest.ContentType   = "application/x-www-form-urlencoded";
        webRequest.ContentLength = byteArray.Length;
        webRequest.Credentials   = CredentialCache.DefaultCredentials;
        webRequest.Timeout       = 5000;
        Stream newStream;

        try
        {
            //3. 附加要POST给服务器的数据到HttpWebRequest对象(附加POST数据的过程比较特殊,它并没有提供一个属性给用户存取,需要写入HttpWebRequest对象提供的一个stream里面。)
            newStream = webRequest.GetRequestStream();            //创建一个Stream,赋值是写入HttpWebRequest对象提供的一个stream里面
            newStream.Write(byteArray, 0, byteArray.Length);
            newStream.Close();
            //4. 读取服务器的返回信息
            HttpWebResponse response;
            response = (HttpWebResponse)webRequest.GetResponse();
            StreamReader php    = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string       phpend = php.ReadToEnd();
            return(JsonMapper.ToObject(phpend));
        }
        catch (Exception)               // 如果上面 连接的时候 有错误 那么直接 跳到二维码界面 返回 连接超时的字符串
        {
            return(null);
        }
    }
Exemplo n.º 2
0
    public RegisterTool(string name)
        : base(name)
    {
        string registerCode = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUBWXYZ";

        REGISTER_CODE = BinaryUtility.stringToBytes(registerCode);
        CODE_LEN      = REGISTER_CODE.Length;
    }
Exemplo n.º 3
0
    protected string getMD5(string source)
    {
        MD5 md5 = new MD5CryptoServiceProvider();

        byte[] sourceBytes = BinaryUtility.stringToBytes(source);
        byte[] result      = md5.ComputeHash(sourceBytes);
        string md5Value    = BinaryUtility.bytesToHEXString(result, false, false);

        return(md5Value);
    }
Exemplo n.º 4
0
    // 生成注册码
    public string generateRegisteCode(string requestCode, string encodeKey)
    {
        byte[] encodeBytes = BinaryUtility.stringToBytes(getMD5(encodeKey));
        // 再次计算MD5
        string retStr = getMD5(requestCode);

        // 然后再加密
        retStr = encode(retStr, encodeBytes);
        return(retStr);
    }
Exemplo n.º 5
0
 public static int getStringLength(string str)
 {
     byte[] bytes = BinaryUtility.stringToBytes(str);
     for (int i = 0; i < bytes.Length; ++i)
     {
         if (bytes[i] == 0)
         {
             return(i);
         }
     }
     return(bytes.Length);
 }
Exemplo n.º 6
0
    public void writeString(string str)
    {
        int strLen = str.Length;

        if (!writeCheck(strLen + sizeof(int)))
        {
            return;
        }
        // 先写入字符串长度
        write(strLen);
        writeBuffer(BinaryUtility.stringToBytes(str), strLen);
    }
Exemplo n.º 7
0
    public static JsonData httpWebRequestPost(string url, string param, OnHttpWebRequestCallback callback = null, object callbakcUserData = null)
    {
        // 转换输入参数的编码类型,获取byte[]数组
        byte[] byteArray = BinaryUtility.stringToBytes(param, Encoding.UTF8);
        // 初始化新的webRequst
        // 1. 创建httpWebRequest对象
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));

        // 2. 初始化HttpWebRequest对象
        webRequest.Method        = "POST";
        webRequest.ContentType   = "application/x-www-form-urlencoded";
        webRequest.ContentLength = byteArray.Length;
        webRequest.Credentials   = CredentialCache.DefaultCredentials;
        webRequest.Timeout       = 10000;
        // 异步
        if (callback != null)
        {
            RequestThreadParam threadParam = new RequestThreadParam();
            threadParam.mRequest   = webRequest;
            threadParam.mByteArray = byteArray;
            threadParam.mCallback  = callback;
            threadParam.mUserData  = callbakcUserData;
            threadParam.mFullURL   = url + param;
            Thread httpThread = new Thread(waitPostHttpWebRequest);
            threadParam.mThread = httpThread;
            httpThread.Start(threadParam);
            httpThread.IsBackground = true;
            mHttpThreadList.Add(httpThread);
            return(null);
        }
        // 同步
        else
        {
            try
            {
                // 3. 附加要POST给服务器的数据到HttpWebRequest对象(附加POST数据的过程比较特殊,它并没有提供一个属性给用户存取,需要写入HttpWebRequest对象提供的一个stream里面。)
                Stream newStream = webRequest.GetRequestStream();                //创建一个Stream,赋值是写入HttpWebRequest对象提供的一个stream里面
                newStream.Write(byteArray, 0, byteArray.Length);
                newStream.Close();
                // 4. 读取服务器的返回信息
                HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
                StreamReader    php      = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                string          phpend   = php.ReadToEnd();
                php.Close();
                response.Close();
                return(JsonMapper.ToObject(phpend));
            }
            catch (Exception)
            {
                return(null);
            }
        }
    }
Exemplo n.º 8
0
    public void writeString(string str)
    {
        int strLen = str.Length;

        if (!writeCheck(strLen + sizeof(int)))
        {
            return;
        }
        // 先写入字符串长度
        write(strLen);
        BinaryUtility.memcpy(mBuffer, BinaryUtility.stringToBytes(str), mIndex, 0, strLen);
        mIndex += strLen;
    }
Exemplo n.º 9
0
    protected string encode(string str, byte[] encodeKeyBytes)
    {
        byte[] strBytes     = BinaryUtility.stringToBytes(str);
        int    byteLen      = strBytes.Length;
        int    encodeKeyLen = encodeKeyBytes.Length;

        for (int i = 0; i < byteLen; ++i)
        {
            sbyte oriByte    = (sbyte)strBytes[i];
            sbyte encodeByte = (sbyte)encodeKeyBytes[i % encodeKeyLen];
            oriByte    ^= encodeByte;
            strBytes[i] = REGISTER_CODE[Mathf.Abs((oriByte + encodeByte) + 0xff) % CODE_LEN];
        }
        str = BinaryUtility.bytesToString(strBytes);
        return(str);
    }
Exemplo n.º 10
0
    public static float calculateFloat(string str)
    {
        // 判断字符串是否含有非法字符,也就是除数字,小数点,运算符以外的字符
        string newString = "";
        int    oldStrLen = str.Length;

        for (int i = 0; i < oldStrLen; ++i)
        {
            if ((str[i] < '0' || str[i] > '9') && str[i] != '.' && str[i] != '+' &&
                str[i] != '-' && str[i] != '*' && str[i] != '/' && str[i] != '(' && str[i] != ')')
            {
                continue;
            }
            else
            {
                newString += str[i];
            }
        }
        str = newString;
        // 判断左右括号数量是否相等
        int leftBracketCount  = 0;
        int rightBracketCount = 0;
        int newStrLen         = str.Length;

        for (int i = 0; i < newStrLen; ++i)
        {
            if (str[i] == '(')
            {
                ++leftBracketCount;
            }
            else if (str[i] == ')')
            {
                ++rightBracketCount;
            }
        }
        if (leftBracketCount != rightBracketCount)
        {
            // 计算错误,左右括号数量不对应
            return(0);
        }

        // 循环判断传入的字符串有没有括号
        while (true)
        {
            // 先判断有没有括号,如果有括号就先算括号里的,如果没有就退出while循环
            if (str.IndexOf("(") != -1 || str.IndexOf(")") != -1)
            {
                int    curpos       = str.LastIndexOf("(");
                string strInBracket = str.Substring(curpos + 1, str.Length - curpos - 1);
                strInBracket = strInBracket.Substring(0, strInBracket.IndexOf(")"));
                float ret = calculateFloat(strInBracket);
                // 如果括号中的计算结果是负数,则标记为负数
                bool isMinus = false;
                if (ret < 0)
                {
                    ret     = -ret;
                    isMinus = true;
                }
                // 将括号中的计算结果替换原来的表达式,包括括号也一起替换
                string floatStr = (Math.Round(ret, 4)).ToString();
                str = StringUtility.strReplace(str, curpos, curpos + strInBracket.Length + 2, floatStr);
                byte[] strchar = BinaryUtility.stringToBytes(str, Encoding.ASCII);
                if (isMinus)
                {
                    // 如果括号中计算出来是负数,则将负号提取出来,将左边的第一个加减号改为相反的符号
                    bool changeMark = false;
                    for (int i = curpos - 1; i >= 0; --i)
                    {
                        // 找到第一个+号,则直接改为减号,然后退出遍历
                        if (strchar[i] == '+')
                        {
                            strchar[i] = (byte)'-';
                            str        = BinaryUtility.bytesToString(strchar, Encoding.ASCII);
                            changeMark = true;
                            break;
                        }
                        // 找到第一个减号,如果减号的左边有数字,则直接改为+号
                        // 如果减号的左边不是数字,则该减号是负号,将减号去掉,
                        else if (strchar[i] == '-')
                        {
                            if (strchar[i - 1] >= '0' && strchar[i - 1] <= '9')
                            {
                                strchar[i] = (byte)'+';
                                str        = BinaryUtility.bytesToString(strchar, Encoding.ASCII);
                            }
                            else
                            {
                                str = StringUtility.strReplace(str, i, i + 1, "");
                            }
                            changeMark = true;
                            break;
                        }
                    }
                    // 如果遍历完了还没有找到可以替换的符号,则在表达式最前面加一个负号
                    if (!changeMark)
                    {
                        str = "-" + str;
                    }
                }
            }
            else
            {
                break;
            }
        }
        List <float> numbers = new List <float>();
        List <char>  factors = new List <char>();
        // 表示上一个运算符的下标+1
        int beginpos = 0;

        for (int i = 0; i < str.Length; ++i)
        {
            // 遍历到了最后一个字符,则直接把最后一个数字放入列表,然后退出循环
            if (i == str.Length - 1)
            {
                string num  = str.Substring(beginpos, str.Length - beginpos);
                float  fNum = float.Parse(num);
                numbers.Add(fNum);
                break;
            }
            // 找到第一个运算符
            if ((str[i] < '0' || str[i] > '9') && str[i] != '.')
            {
                if (i != 0)
                {
                    string num  = str.Substring(beginpos, i - beginpos);
                    float  fNum = float.Parse(num);
                    numbers.Add(fNum);
                }
                // 如果在表达式的开始就发现了运算符,则表示第一个数是负数,那就处理为0减去这个数的绝对值
                else
                {
                    numbers.Add(0);
                }
                factors.Add(str[i]);
                beginpos = i + 1;
            }
        }
        if (factors.Count + 1 != numbers.Count)
        {
            // 计算错误,运算符与数字数量不符
            return(0);
        }
        // 现在开始计算表达式,按照运算优先级,先计算乘除和取余
        while (true)
        {
            // 表示是否还有乘除表达式
            bool hasMS = false;
            for (int i = 0; i < (int)factors.Count; ++i)
            {
                // 先遍历到哪个就先计算哪个
                if (factors[i] == '*' || factors[i] == '/')
                {
                    // 第一个运算数的下标与运算符的下标是相同的
                    float num1 = numbers[i];
                    float num2 = numbers[i + 1];
                    float num3 = 0.0f;
                    if (factors[i] == '*')
                    {
                        num3 = num1 * num2;
                    }
                    else if (factors[i] == '/')
                    {
                        num3 = num1 / num2;
                    }
                    // 删除第i + 1个数,然后将第i个数替换为计算结果
                    numbers.RemoveAt(i + 1);
                    if (numbers.Count == 0)
                    {
                        // 计算错误
                        return(0);
                    }
                    numbers[i] = num3;
                    // 删除第i个运算符
                    factors.RemoveAt(i);
                    hasMS = true;
                    break;
                }
            }
            if (!hasMS)
            {
                break;
            }
        }
        // 再计算加减法
        while (true)
        {
            if (factors.Count == 0)
            {
                break;
            }
            if (factors[0] == '+' || factors[0] == '-')
            {
                // 第一个运算数的下标与运算符的下标是相同的
                float num1 = numbers[0];
                float num2 = numbers[1];
                float num3 = 0.0f;
                if (factors[0] == '+')
                {
                    num3 = num1 + num2;
                }
                else if (factors[0] == '-')
                {
                    num3 = num1 - num2;
                }
                // 删除第1个数,然后将第0个数替换为计算结果
                numbers.RemoveAt(1);
                if (numbers.Count == 0)
                {
                    // 计算错误
                    return(0);
                }
                numbers[0] = num3;
                // 删除第0个运算符
                factors.RemoveAt(0);
            }
        }
        if (numbers.Count != 1)
        {
            // 计算错误
            return(0);
        }
        else
        {
            return(numbers[0]);
        }
    }
Exemplo n.º 11
0
 public void setName(string name)
 {
     byte[] nameBytes = BinaryUtility.stringToBytes(name, Encoding.UTF8);
     mName.setValue(nameBytes);
 }
Exemplo n.º 12
0
 public void setPassword(string password)
 {
     byte[] passwordBytes = BinaryUtility.stringToBytes(password);
     mPassword.setValue(passwordBytes);
 }
Exemplo n.º 13
0
 public void setAccount(string account)
 {
     byte[] accountBytes = BinaryUtility.stringToBytes(account);
     mAccount.setValue(accountBytes);
 }