public WebBrowserHook(WebBrowser webBrowser) { _webBrowser = webBrowser; _webBrowser.Loaded += (s, e) => { try { var axWebBrowser = (SHDocVw.WebBrowser)GetActiveXInstance(this._webBrowser); axWebBrowser.DownloadBegin += delegate { HandleDownloadActivity(); }; axWebBrowser.NavigateComplete2 += delegate(object pDisp, ref object URL) { // top frame? if (Object.ReferenceEquals(axWebBrowser, pDisp)) { this._isNavigating = true; HandleDownloadActivity(); } }; } catch (Exception ex) { AppLog.LogException(ex); } }; }
// handler for document.readyState == "complete" private void DomDocumentCompleteHandler(mshtml.HTMLDocument domDocument) { try { mshtml.HTMLWindow2 domWindow = (mshtml.HTMLWindow2)domDocument.parentWindow; domWindow.attachEvent("onunload", new DomEventHandler(delegate { this._isLoaded = false; this._isLoading = false; })); var navigated = this._isNavigating; this._isNavigating = false; this._isLoaded = true; this._isLoading = false; if (navigated) { if (Navigated != null) { Navigated(this, new EventArgs()); } } else { if (Refreshed != null) { Refreshed(this, new EventArgs()); } } } catch (Exception ex) { AppLog.LogException(ex); } }
private static void Request(string serviceUrl, Dictionary <string, string> postFields, Action <string> callback) { IObservable <WWW> wWW; Dictionary <string, string> strs; WWWForm wWWForm = null; if (postFields != null) { wWWForm = new WWWForm(); foreach (KeyValuePair <string, string> postField in postFields) { if (postField.Key == null) { throw new NullReferenceException(string.Format("Post field key is null. URL: {0}\nFields:\n{1}", serviceUrl, WebService.GetFieldsString(postFields))); } if (postField.Value == null) { throw new NullReferenceException(string.Format("Post field value for key '{0}' is null. URL: {1}\nFields:\n{2}", postField.Key, serviceUrl, WebService.GetFieldsString(postFields))); } wWWForm.AddField(postField.Key, postField.Value); } } string str = Uri.EscapeUriString(serviceUrl); if (wWWForm == null) { strs = new Dictionary <string, string>() { { "ACCEPT-ENCODING", "gzip" } }; wWW = ObservableWWW.GetWWW(str, strs, null); } else { strs = new Dictionary <string, string>() { { "ACCEPT-ENCODING", "gzip" } }; wWW = ObservableWWW.PostWWW(str, wWWForm, strs, null); } wWW.Retry <WWW>(5).CatchIgnore <WWW, WWWErrorException>((WWWErrorException ex) => { AppLog.LogError(string.Format("WWW error: {0}", ex.RawErrorMessage), true); callback(null); }).Select <WWW, string>((WWW www) => { string empty = string.Empty; www.responseHeaders.TryGetValue("CONTENT-ENCODING", out empty); if (empty != "gzip") { return(www.text); } return(WebService.Ungzip(www.bytes)); }).Subscribe <string>(callback, (Exception e) => { AppLog.LogException(e, true); callback(null); }); }
void Security.ISkyTrakSW.ScanWiFiNetworks() { try { this._wrapper.ScanWiFiNetwork(); } catch (Exception exception1) { Exception exception = exception1; AppLog.LogException(exception, true); throw exception; } }
private void ProcessRequest(int index) { Action <Exception> action1 = null; WebService.QueuedRequest queuedRequest = this._currentRequests[index]; Guid guid = Guid.NewGuid(); string serverRequestUrl = WebService.GetServerRequestUrl(queuedRequest.Service, queuedRequest.Request, queuedRequest.IsSkyTrakCall); object[] service = new object[] { queuedRequest.Service, queuedRequest.Request, guid, serverRequestUrl, null, null }; service[4] = (queuedRequest.PostFields.Count != 0 ? string.Format("{0}?{1}", serverRequestUrl, ( from kvp in queuedRequest.PostFields select string.Format("{0}={1}", kvp.Key, kvp.Value)).Aggregate <string>((string s1, string s2) => string.Concat(s1, "&", s2))) : serverRequestUrl); service[5] = (queuedRequest.PostFields.Count != 0 ? ( from kvp in queuedRequest.PostFields select string.Format("{{ {0}, {1} }}", kvp.Key, kvp.Value)).Aggregate <string>((string s1, string s2) => string.Concat(s1, ",\n", s2)) : string.Empty); AppLog.Log(string.Format("=== CallWebService. Service: {0}. Request: {1}. ID: {2}\nURL {3}\nGET: {4}\nData:\n{5}", service), true); WebService.Request(serverRequestUrl, queuedRequest.PostFields, (string response) => { string str; string str1; string str2; bool flag; AppLog.Log(string.Format("=== CallWebService Response. Service: {0}. Request: {1}. ID: {2}\nURL {3}\nData:\n{4}", new object[] { queuedRequest.Service, queuedRequest.Request, guid, serverRequestUrl, response }), true); if (string.IsNullOrEmpty(response)) { NetworkInternetVerification.instance.Reset(); } WebService.ParseStatus(response, out str2, out str, out str1, out flag); Action callback = () => queuedRequest.Callback(new WebServiceResponse(queuedRequest.Service, queuedRequest.Request, response, str, str1, str2, flag)); if (!queuedRequest.CallbackInParallelThread) { callback(); } else { Action action = callback; if (action1 == null) { action1 = (Exception ex) => AppLog.LogException(ex, true); } ThreadingUtilities.Run(action, null, action1); } this._currentRequests[index] = null; }); }
private void HandleDownloadActivity() { try { mshtml.HTMLDocument domDocument = (mshtml.HTMLDocument) this._webBrowser.Document; if (domDocument == null) { return; } if (_isLoading || _isLoaded) { return; } this._isLoading = true; if (domDocument.readyState == "complete") { DomDocumentCompleteHandler(domDocument); } else { DomEventHandler handler = null; handler = new DomEventHandler(delegate { if (domDocument.readyState == "complete") { domDocument.detachEvent("onreadystatechange", handler); DomDocumentCompleteHandler(domDocument); } }); domDocument.attachEvent("onreadystatechange", handler); } } catch (Exception ex) { AppLog.LogException(ex); } }
private static string Ungzip(byte[] bytes) { string end; try { using (MemoryStream memoryStream = new MemoryStream(bytes)) { using (GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress)) { using (StreamReader streamReader = new StreamReader(gZipStream, Encoding.UTF8)) { end = streamReader.ReadToEnd(); } } } } catch (Exception exception) { AppLog.LogException(exception, true); end = null; } return(end); }