public static AgentEventData GetAgentEvents(string agentid) { string uri = BaseUri + string.Format(AgentGatewayUri.AGENTEVENT_URI, agentid); SetSecurityProtocolByUrl(uri); string strInput = string.Empty; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.Timeout = 20000; request.KeepAlive = false; request.ContentType = @"application/json"; request.Method = HttpMethod.GET.ToString(); if (!string.IsNullOrEmpty(guid)) { Add(request, "guid", guid); } if (lst_cookie != null && lst_cookie.Count > 0) { foreach (var cookie in lst_cookie) { Add(request, HttpRequestHeader.Cookie, cookie); } } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { string tmpGuid = response.Headers.Get("Set-GUID"); if (!string.IsNullOrEmpty(tmpGuid)) { tmpGuid = tmpGuid.Substring(tmpGuid.IndexOf("=") + 1); } } using (Stream rs = response.GetResponseStream()) { List <byte> bts = new List <byte>(); int tmp; while ((tmp = rs.ReadByte()) >= 0) { bts.Add((byte)tmp); } strInput = System.Text.UTF8Encoding.UTF8.GetString(bts.ToArray()); if (string.Equals("{\"message\":\"\",\"retcode\":\"0\"}", strInput))//屏蔽AGW返回的空的事件 { return(new AgentEventData() { IsSuccess = true, CallBackFunctionName = null, Data = strInput }); } AgentEventData data = new AgentEventData(); var jObject = Parse(strInput); var @event = jObject["event"]; if (@event == null) { data.IsSuccess = false; data.Data = strInput; data.CallBackFunctionName = null; return(data); } else { data.IsSuccess = true; var eventObject = Parse(@event.ToString()); var eventType = eventObject["eventType"]; data.Data = strInput; data.CallBackFunctionName = eventType.ToString(); return(data); } } } catch (WebException webexc) { SpecError = webexc.Message; if (!string.IsNullOrEmpty(BackupUrl) && !string.Equals(BaseUri, BackupUrl)) { BaseUri = BackupUrl; } return(null); } }
static AgentGatewayHelper() { t.Elapsed += (a, b) => { t.Stop(); if (!string.IsNullOrEmpty(currentWorkNo)) { //需要内部做的逻辑处理 try { AgentEventData eventData = AgentGatewayHelper.GetAgentEvents(currentWorkNo); if (null == eventData) { callBackFailedTimes++; if (callBackFailedTimes >= maxCallBackFailedTimes) { ForceOutAndClearInfo(); } } else { string data = eventData.Data; var jObject = Parse(data); var retcode = jObject["retcode"]; if (retcode == null || !eventData.IsSuccess || retcode.ToString() == AGWErrorCode.NoRight || retcode.ToString() == AGWErrorCode.NotLogIn) { callNoRightTimes++; if (callNoRightTimes >= maxNoRightMaxTimes) { ForceOutAndClearInfo(); } } if (!string.IsNullOrEmpty(eventData.CallBackFunctionName)) { callBackFailedTimes = 0; callNoRightTimes = 0; if (CallBackEvent != null) { new Thread( () => { CallBackEvent(null, eventData); } ).Start(); } } } } catch (AgentGatewayException agex) { SpecError = agex.Message; callNoRightTimes++; if (callNoRightTimes >= maxNoRightMaxTimes) { ForceOutAndClearInfo(); } } catch (Exception ex) { SpecError = ex.Message; if (!string.IsNullOrEmpty(BackupUrl) && !string.Equals(BaseUri, BackupUrl)) { BaseUri = BackupUrl; } callBackFailedTimes++; if (callBackFailedTimes >= maxCallBackFailedTimes) { ForceOutAndClearInfo(); } } } t.Start(); }; t.Interval = 500; t.Start(); }