Example #1
0
    /// <summary>
    /// 开放给Lua端调用的Post接口
    /// </summary>
    /// <param name="url">URL</param>
    /// <param name="values">值表</param>
    /// <param name="files">文件表,会以二进制的形式向服务端请求(通过WWWForm.AddBinaryData的形式)</param>
    /// <param name="cb">回调</param>
    public void POST(string url, XLua.LuaTable values, XLua.LuaTable files, POSTCB cb)
    {
        Dictionary <string, string> data = new Dictionary <string, string>();
        IDictionaryEnumerator       de   = null;

        if (values != null)
        {
            //de = values.GetEnumerator();
            //while (de.MoveNext())
            //{
            //    data.Add(de.Key as string, de.Value as string);
            //}
            foreach (string key in values.GetKeys <string>())
            {
                data.Add(key, values.Get <string>(key));
            }
        }

        Dictionary <string, KeyValuePair <string, byte[]> > stream = new Dictionary <string, KeyValuePair <string, byte[]> >();

        if (files != null)
        {
            //de = files.GetEnumerator();
            //while (de.MoveNext())
            //{
            //    string field = de.Key as string;
            //    string path = de.Value as string;
            //    if (string.IsNullOrEmpty(path) || !File.Exists(path))
            //    {
            //        GameDebug.LogWarning(string.Format("Unexpect path:{0}", path));
            //        continue;
            //    }
            //    byte[] bytes = File.ReadAllBytes(path);
            //    stream.Add(field, new KeyValuePair<string, byte[]>(Path.GetFileName(path), bytes));
            //}
            foreach (string field in files.GetKeys <string>())
            {
                string path = values.Get <string>(field);
                if (string.IsNullOrEmpty(path) || !File.Exists(path))
                {
                    GameDebug.LogWarning(string.Format("Unexpect path:{0}", path));
                    continue;
                }
                byte[] bytes = File.ReadAllBytes(path);
                stream.Add(field, new KeyValuePair <string, byte[]>(Path.GetFileName(path), bytes));
            }
        }

        POST(url, data, stream, cb, null);
    }
Example #2
0
 public void POST(string url, Dictionary <string, string> data, Dictionary <string, KeyValuePair <string, byte[]> > stream, POSTCB cb, System.Object userData)
 {
     StartCoroutine(_POST(url, data, stream, cb, userData));
 }
Example #3
0
 public void POST(string url, Dictionary <string, string> data, POSTCB cb)
 {
     POST(url, data, cb, null);
 }
Example #4
0
 public void POST(string url, Dictionary <string, string> data, POSTCB cb, System.Object userData)
 {
     StartCoroutine(_POST(url, data, null, cb, userData));
 }
Example #5
0
    IEnumerator _POST(string url, Dictionary <string, string> data, Dictionary <string, KeyValuePair <string, byte[]> > streams, POSTCB cb, System.Object userData)
    {
        //GameDebug.Log("HttpRequest Post, url: " + url + ", data: " + data);

        WWWForm form = new WWWForm();

        foreach (KeyValuePair <string, string> pair in data)
        {
            //GameDebug.Log(string.Format("{0}:{1}", pair.Key, pair.Value));
            form.AddField(pair.Key, pair.Value);
        }
        if (form.data.Length == 0)
        {
            form.AddField("a", "a");
        }

        foreach (KeyValuePair <string, KeyValuePair <string, byte[]> > pair in streams)
        {
            //GameDebug.Log(string.Format("{0}:{1}:{2}", pair.Key, pair.Value.Value.Length, pair.Value.Key));
            form.AddBinaryData(pair.Key, pair.Value.Value, pair.Value.Key);
        }

        WWW www = new WWW(url, form);

        yield return(www);

        if (string.IsNullOrEmpty(www.error) == false)
        {
            //GameDebug.LogError("HttpRequest Post URL " + url + " error! " + www.error);
            if (cb != null)
            {
                cb(url, data, www.error, "", userData);
            }
        }
        else
        {
            //GameDebug.Log("HttpRequest Get callback, url: " + url + ", reply: " + www.text);
            if (cb != null)
            {
                cb(url, data, www.error, www.text, userData);
            }
        }
    }