Example #1
0
 private Action <WWW> RespWrapper(OnHttpResponse rawCb)
 {
     return((WWW www) =>
     {
         rawCb(GenerateResp(www));
     });
 }
Example #2
0
        public void Post(string resource, string jsonString, OnHttpResponse callback)
        {
            var fullURL = GetHttpPrefix() + resource;
            var headers = GetAuthHeader();

            headers.Add("Content-Type", "application/json");

            var postJson = jsonString;//.ToString();

            InfoTips.LogInfo("[HttpPost] " + fullURL);
            InfoTips.LogInfo("postJson:" + postJson + "--Headers:" + headers);

            var formData = Encoding.UTF8.GetBytes(jsonString);

            var req = new WWWRequest()
            {
                Data         = formData,
                Headers      = headers,
                RespCallback = RespWrapper(callback),
                Type         = RequestType.Post,
                Url          = fullURL
            };

            waitingRequests.Enqueue(req);
        }
 /// <summary>
 /// GET
 /// </summary>
 /// <param name="url"></param>
 /// <param name="onResponse"></param>
 /// <param name="onProgress"></param>
 /// <param name="TimeOut"></param>
 public static void Get(string url, OnHttpResponse onResponse, OnProgress onProgress = null, float TimeOut = -1)
 {
     ins._reqests.Add(new HttpRequest()
     {
         www        = new WWW(url),
         onDone     = onResponse,
         onProgress = onProgress,
         AddTime    = Time.realtimeSinceStartup,
         TimeOut    = TimeOut
     });
 }
Example #4
0
        /// <summary>
        /// 登录,其他所有操作都要在登录成功后进行
        /// 跟其他http谓词都不一样,没有排队,直接发送了
        /// </summary>
        public void Login(string name, string password, OnHttpResponse cb)
        {
            var queryString       = string.Format("email={0}&password={1}", name, password);
            var fullURL           = LoginAddress + "?" + queryString;
            var seemsUselessBytes = Encoding.UTF8.GetBytes(queryString);

            var req = new WWWRequest()
            {
                Data         = seemsUselessBytes,
                Headers      = null,
                RespCallback = www =>
                {
                    if (www == null)
                    {
                        cb(GenTimeoutResp());
                        return;
                    }

                    var resp = new HttpResp();

                    if (!string.IsNullOrEmpty(www.error))
                    {
                        resp.Error     = HttpResp.ErrorType.NetworkError;
                        resp.ErrorText = www.error;
                    }
                    else
                    {
                        var loginResp = JsonUtility.FromJson <LoginResp>(www.text);

                        if (!string.IsNullOrEmpty(loginResp.error))
                        {
                            resp.Error     = HttpResp.ErrorType.LogicError;
                            resp.ErrorText = loginResp.error;
                        }
                        else
                        {
                            resp.Error = HttpResp.ErrorType.None;

                            //set global identity
                            userId      = loginResp.user_id;
                            accessToken = loginResp.access_token;
                        }
                    }

                    resp.WwwText = www.text;
                    cb(resp);
                },
                Type = RequestType.Login,
                Url  = fullURL
            };

            waitingRequests.Enqueue(req);
        }
Example #5
0
        /// <summary>
        /// 普通get
        /// </summary>
        public void Get(string resource, NameValueCollection query, OnHttpResponse callback)
        {
            var fullURL = GetHttpPrefix() + resource + QueryToString(query);

            InfoTips.LogInfo("[HttpGet] " + fullURL);

            var req = new WWWRequest()
            {
                Data         = null,
                Headers      = GetAuthHeader(),
                RespCallback = RespWrapper(callback),
                Type         = RequestType.Get,
                Url          = fullURL
            };

            waitingRequests.Enqueue(req);
        }
 /// <summary>
 /// 简易Post,(自动使用默认的超时时间)
 /// </summary>
 /// <param name="url"></param>
 /// <param name="form"></param>
 /// <param name="onResponse"></param>
 /// <param name="onProgress"></param>
 public static void SimplePost(string url, WWWForm form, OnHttpResponse onResponse, OnProgress onProgress = null)
 {
     Post(url, form, onResponse, onProgress, DEFAUT_TIMEOUT);
 }
 /// <summary>
 /// 简易GET,自动使用默认超时时间
 /// </summary>
 /// <param name="url"></param>
 /// <param name="onResponse"></param>
 /// <param name="onProgress"></param>
 public static void SimpleGet(string url, OnHttpResponse onResponse, OnProgress onProgress = null)
 {
     Get(url, onResponse, onProgress, DEFAUT_TIMEOUT);
 }
Example #8
0
 /// <summary>
 /// 普通post
 /// </summary>
 public void Post(string resource, NameValueCollection query, OnHttpResponse callback)
 {
     Post(resource, ToJson(query), callback);
 }
Example #9
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="standard">标准的上传文件接口,都会默认在query里带上name和crc</param>
        public void Post(string resource, NameValueCollection query, string fileName, byte[] fileData, OnHttpResponse callback, bool standard = true)
        {
            string fullURL = GetHttpPrefix() + resource;

            var postParameters = new Dictionary <string, object>();

            // 标准模式:只需要name,crc
            if (standard)
            {
                postParameters.Add("name", query["name"]);
                postParameters.Add("crc", query["crc"]);
            }
            // Post过去所有的键值对
            else
            {
                foreach (var key in query.AllKeys)
                {
                    postParameters.Add(key, query[key]);
                }
            }
            postParameters.Add("file", new FormUpload.FileParameter(fileData, fileName, "application/octet-stream"));

            var    headers = GetAuthHeader();
            string contentType;
            var    bytes = FormUpload.GetHTTPBody(postParameters, out contentType);

            headers.Add("Content-Type", contentType);

            var req = new WWWRequest()
            {
                Data         = bytes,
                Headers      = headers,
                RespCallback = RespWrapper(callback),
                Type         = RequestType.Post,
                Url          = fullURL
            };

            waitingRequests.Enqueue(req);
        }