public static void Callback(SynchronizationContext context, string url, byte[] data, Action <Exception, byte[]> resultHandler) { MyWebRequest req = null; try { req = new MyWebRequest(HttpWebRequest.Create(url)); } catch (Exception ex) { context.Post(_ => { resultHandler(ex, null); }, null); return; } req.Request.Method = "POST"; if ((null == data) || (data.Length == 0)) { req.Request.ContentLength = 0; ReceiveCallback(context, req, resultHandler); } else { req.Request.ContentType = "application/json"; try { data = CryptEncoder.Build(data); req.Request.ContentLength = data.Length; req.Request.BeginGetRequestStream(new AsyncCallback(result => { Stream stream = null; try { stream = req.Request.EndGetRequestStream(result); stream.Write(data, 0, data.Length); stream.Close(); } catch (Exception ex) { context.Post(_ => { resultHandler(ex, null); }, null); return; } ReceiveCallback(context, req, resultHandler); }), null); } catch (Exception ex) { context.Post(_ => { resultHandler(ex, null); }, null); } } }
private static void Send(SynchronizationContext context, string url, string method, byte[] data, object userState, Func <Exception, byte[], bool, object, bool> resultHandler) { MyWebRequest req = null; try { req = new MyWebRequest(HttpWebRequest.Create(url)); } catch (Exception ex) { if (null != resultHandler) { context.Post(_ => { resultHandler(ex, null, false, userState); }, null); } return; } //BusyToken.AddRequest(req); //BusyToken.Show(); req.Request.Method = method; if ((null == data) || (data.Length == 0)) { req.Request.ContentLength = 0; Receive(context, req, userState, resultHandler); } else { req.Request.ContentType = "application/json"; try { data = CryptEncoder.Build(data); req.Request.ContentLength = data.Length; req.Request.BeginGetRequestStream(new AsyncCallback(result => { Stream stream = null; try { stream = req.Request.EndGetRequestStream(result); stream.Write(data, 0, data.Length); stream.Close(); } catch (Exception ex) { context.Post(_ => { //BusyToken.Hide(); if (null != resultHandler) { resultHandler(ex, null, req.IsCancel, userState); } }, null); return; } Receive(context, req, userState, resultHandler); }), null); } catch (Exception ex) { context.Post(_ => { //BusyToken.Hide(); if (null != resultHandler) { resultHandler(ex, null, req.IsCancel, userState); } }, null); } } }