public SimpleHttpReq(System.Uri uri, SimpleHttpCallback callback, int reqId = int.MinValue) { ReqId = reqId; Req = new HTTPRequest(uri, (req, res) => { CallbackConvert(this, res, callback); }); }
public SimpleHttpReq(System.Uri uri, bool isKeepAlive, bool disableCache, SimpleHttpCallback callback, int reqId = int.MinValue) { ReqId = reqId; Req = new HTTPRequest(uri, isKeepAlive, disableCache, (req, res) => { CallbackConvert(this, res, callback); }); }
public static void CallbackConvert(SimpleHttpReq simpleReq, HTTPResponse res, SimpleHttpCallback callback) { // 0.如果 callbacck 为 null 则直接 return if (callback == null) { return; } // 1.请求成功则直接 callback if (res != null) { if (res.StatusCode == 200 || res.StatusCode == 304) { callback(SimpleHttpResult.Success, res, simpleReq.ReqId); return; } else if (res.StatusCode == 400) { // code 400 说明服务器正常 但是 数据有错误 所以 不需要再 retry 了 VeerDebug.LogWarning(" req failed with code 400 : " + simpleReq.Req.Uri + " error msg : " + res.Message); callback(SimpleHttpResult.FailedFor400, res, simpleReq.ReqId); return; } else if (res.StatusCode == 404) { // code 404 地址不存在 不需要再 retry 了 //VeerDebug.LogWarning(" req failed with code 404 : " + simpleReq.Req.Uri + " error msg : " + res.Message); callback(SimpleHttpResult.FailedForNetwork, res, simpleReq.ReqId); return; } } // 2.Abort 则 直接 callback if (simpleReq.Req.State == HTTPRequestStates.Aborted) { callback(SimpleHttpResult.Abort, res, simpleReq.ReqId); return; } // 3.其它情况需要尝试 retry if (simpleReq._CurrentRetryCount < simpleReq._RetryCount) { simpleReq._CurrentRetryCount++; VeerDebug.Log(" retry http req time : " + simpleReq._CurrentRetryCount + " api : " + simpleReq.Req.Uri + " code : " + res.StatusCode); simpleReq.Send(true); return; } // 4.retry 结束后返回 callback if (simpleReq.Req.State == HTTPRequestStates.Error) { VeerDebug.Log(" request error : " + simpleReq.Req.Exception); callback(SimpleHttpResult.FailedForNetwork, res, simpleReq.ReqId); return; } if (simpleReq.Req.State == HTTPRequestStates.ConnectionTimedOut || simpleReq.Req.State == HTTPRequestStates.TimedOut) { VeerDebug.Log(" request time out ..."); callback(SimpleHttpResult.FailedForNetwork, res, simpleReq.ReqId); return; } callback(SimpleHttpResult.FailedForNetwork, res, simpleReq.ReqId); }