private void CheckResponseError(SnapRpcDataVO rpcData) { if (!rpcData.status) { LogManager.LogError("[HttpRequest]Check Response Error! error code: " , rpcData.code, ", err message: ", rpcData.message); if (rpcData.code == (int)HttpErrorCode.LoginTokenError) { GameManager.LoginOnOtherDivices(false); } } SnapHttpManager.getInstance().AddOneHttpCallBack(httpRequestVO.requestBackAction, rpcData); }
public static HttpWebRequest PutRequest(SnapRequestVO vo, Action <SnapRpcDataVO> callback = null) { string request_url = GetHttpURL(vo); if (string.IsNullOrEmpty(request_url)) { request_url = SnapHttpConfig.NET_APP_URL; } byte[] body = vo.toByte(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(request_url); request.Proxy = null; //LogManager.Log(request.RequestUri); request.Method = SnapHttpConfig.NET_REQUEST_PUT; request.Timeout = 600 * 1000; request.ReadWriteTimeout = 600 * 1000; request.ContentType = SnapAppUploadFile.OSS_ContentType; request.ContentLength = body.Length; if (vo.mp3byte != null && vo.mp3byte.Length > 0) { MethodInfo priMethod = request.Headers.GetType().GetMethod("AddWithoutValidate", BindingFlags.Instance | BindingFlags.NonPublic); priMethod.Invoke(request.Headers, new[] { "Date", SnapAppUploadFile.OSS_Date }); priMethod.Invoke(request.Headers, new[] { "Content-Length", body.Length.ToString() }); priMethod.Invoke(request.Headers, new[] { "Content-Type", SnapAppUploadFile.OSS_ContentType }); request.Headers.Add("Authorization", SnapAppUploadFile.OSS_Authorization); } else { request.Headers.Add("Token", SnapHttpConfig.NOT_LOGINED_APP_TOKEN); request.Headers.Add("UserToken", SnapHttpConfig.LOGINED_APP_TOKEN); } try { if (body != null && body.Length > 0) { using (Stream stream = request.GetRequestStream()) { stream.Write(body, 0, body.Length); stream.Flush(); stream.Close(); } } if (callback != null) { //音频文件 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { if (response.StatusCode == HttpStatusCode.OK) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { SnapRpcDataVO rpcData = new SnapRpcDataVO(); rpcData.code = 1; rpcData.message = "request success"; rpcData.data = new JsonData(); rpcData.data["uploadUrl"] = response.ResponseUri.AbsolutePath; //if (callback != null) //{ // callback.Invoke(rpcData); // callback = null; //} SnapHttpManager.getInstance().AddOneHttpCallBack(callback, rpcData); reader.Close(); } } response.Close(); request.Abort(); } } } catch (Exception ex) { LogManager.Log(ex.Message); throw; } return(request); }
public static HttpErrorCodeDelegate RequestErrorCodeBack = null; //登录回调 /// <summary> /// 请求App新版本的api /// </summary> /// <param name="requestInterType">请求接口名称,枚举值</param> /// <param name="requestMethod">请求接口方法SnapHttpConfig.NET_REQUEST_GET/SnapHttpConfig.NET_REQUEST_POST</param> /// <param name="jsonData">发送请求的数据</param> /// <param name="callBackAction">服务器返回数据的callback事件</param> /// <param name="priority">发送请求的优先级</param> static public void Request_SnapAppApi(SnapAppApiInterface requestInterType, string requestMethod, JsonData jsonData = null, Action <SnapRpcDataVO> callBackAction = null, int priority = 0, SnapAppApiThirdRequestFace third = SnapAppApiThirdRequestFace.No_Third) { string apiName = ""; switch (requestInterType) { case SnapAppApiInterface.Request_LoginApp: apiName = User_Service_Login; break; case SnapAppApiInterface.Request_GetUserInfo: apiName = User_Service_Get_Info; break; case SnapAppApiInterface.Request_RegisterApp: apiName = User_Service_Register; break; case SnapAppApiInterface.Request_RegisterGuestApp: apiName = Guest_Service_Register; break; case SnapAppApiInterface.Request_CheckUserNameApp: apiName = User_Service_CheckUserName; break; case SnapAppApiInterface.Request_ModifyPwdApp: apiName = User_Service_ModifyPwd; break; case SnapAppApiInterface.Request_AliyunOSSApp: apiName = Request_Aliyun_OSS; break; case SnapAppApiInterface.Request_ThirdWebToApp: //第三方的都走这个,需要什么数据自己封装 apiName = jsonData.TryGetString("apiName"); break; case SnapAppApiInterface.Request_WeChatShareGame: apiName = User_Service_WeChatShare; break; case SnapAppApiInterface.Request_UploadGameInfo: apiName = User_Service_UploadGame_Info + "/" + jsonData.TryGetString("uid"); break; case SnapAppApiInterface.Request_UpdateUserInfo: apiName = User_Service_UpdateUserInfo; break; case SnapAppApiInterface.Request_GetGuestUserInfoByDeviceId: apiName = Guest_Service_GetInfoByDeviceId; break; case SnapAppApiInterface.Request_GetLevelScore: apiName = User_Service_GetLevelScore; break; case SnapAppApiInterface.Request_GetRankScoresByLevelID: apiName = User_Service_GetRankScores + "/" + jsonData.TryGetString("levelID"); break; case SnapAppApiInterface.Request_GetRankOfTenByIDs: apiName = User_Service_GetRankOfTenByID; break; case SnapAppApiInterface.Request_UploadGameScores: apiName = User_Service_UploadGameScores + "/"; break; case SnapAppApiInterface.Request_IAPGetGoodsInfo: apiName = User_IAP_GetGoodsInfo; break; case SnapAppApiInterface.Request_IAPGetGoodsVerify: apiName = User_IAP_Verify; break; case SnapAppApiInterface.Request_CurAppVersion: apiName = App_GetCurrentVersion; break; case SnapAppApiInterface.Request_UploadLevelVoices: apiName = User_Service_UploadLevelVoices; break; case SnapAppApiInterface.Request_GetLevelVoices: apiName = User_Service_UploadLevelVoices; break; } //SnapHttpConfig.REQUEST_APP_API_TOKEN = (apiName.IndexOf (User_Anonymous_Header) != -1) ? //SnapHttpConfig.NOT_LOGINED_APP_TOKEN : SnapHttpConfig.LOGINED_APP_TOKEN; SnapHttpManager.getInstance().Request_SnapAppApi(requestInterType, apiName, requestMethod, jsonData, callBackAction, priority, third); }
private void Update() { SnapHttpManager.getInstance().update(); }
/// <summary> /// 获取http请求返回的数据 /// </summary> private void GetHttpResponse(IAsyncResult result) { HttpWebRequest req = (HttpWebRequest)result.AsyncState; using (response = (HttpWebResponse)req.GetResponse()) { GetServerTime(response.Headers); if (response.StatusCode == HttpStatusCode.OK) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { JsonData jsonData = JsonMapper.ToObject(new JsonReader(reader.ReadToEnd())); SnapRpcDataVO rpcData = new SnapRpcDataVO(); if (jsonData != null) { rpcData.action = httpRequestVO.requestAction; rpcData.data = jsonData["data"]; rpcData.status = (bool)jsonData["status"]; if (rpcData.status == true) { rpcData.code = 1; } else { rpcData.code = int.Parse(jsonData.TryGetString("code")); } rpcData.message = jsonData["message"].ToString(); } close(); LogManager.LogWarning(" - Result:", JsonMapper.ToJson(jsonData)); CheckResponseError(rpcData); //if (httpRequestVO.requestBackAction != null) //{ // httpRequestVO.requestBackAction.Invoke(rpcData); // httpRequestVO.requestBackAction = null; //} } } else { LogManager.Log(response.StatusCode); close(); if (response.StatusCode == HttpStatusCode.RequestTimeout || response.StatusCode == HttpStatusCode.GatewayTimeout) { //需要重新请求的错误异常 if (needReload) { LogManager.Log("reconnect"); if (httpRequestVO.requestErrCount < SnapHttpConfig.NET_RETRY_NUM) { httpRequestVO.requestErrCount++; SnapHttpManager.getInstance().Re_Request_SnapAppApi_Inner(httpRequestVO); } } } else { //全局异常 if (SnapAppApi.RequestErrorCodeBack != null) { SnapAppApi.RequestErrorCodeBack.Invoke((int)response.StatusCode); } } } } }