private void EndInvoke(AsynHttpContext _context, Exception e) { try { _watch.Stop(); _context.ExecTime = _watch.ElapsedMilliseconds; _action.Invoke(_context, e); } catch (Exception ex) { // _logger.Error(ex, "EndInvoke exception."); _action.Invoke(null, ex); } }
private void RequestCompleted(IAsyncResult asyncResult) { AsynHttpContext _context = asyncResult.AsyncState as AsynHttpContext; try { if (asyncResult == null) { return; } _context.AsynResponse = (HttpWebResponse)_context.AsynRequest.EndGetResponse(asyncResult); } catch (WebException e) { // _logger.Error(e, "RequestCompleted WebException."); _context.AsynResponse = (HttpWebResponse)e.Response; if (_context.AsynResponse == null) { EndInvoke(_context, e); return; } } catch (Exception e) { //_logger.Error(e, "RequestCompleted exception."); EndInvoke(_context, e); return; } try { AsynStream stream = new AsynStream(_context.AsynResponse.GetResponseStream()); stream.Offset = 0; long length = _context.AsynResponse.ContentLength; if (length < 0) { length = DEFAULT_LENGTH; notContentLength = true; } buffer = new byte[length]; stream.UnreadLength = buffer.Length; IAsyncResult asyncRead = stream.NetStream.BeginRead(buffer, 0, stream.UnreadLength, new AsyncCallback(ReadCallBack), stream); } catch (Exception e) { // _logger.Error(e, "BeginRead exception."); EndInvoke(_context, e); } }
public static void Post(AsynHttpContext context, string reqStr, Action <AsynHttpContext, Exception> callBack) { AysncHttpRequestHelperV2 proxy = new AysncHttpRequestHelperV2(context, callBack); proxy.SendRequest(Encoding.UTF8.GetBytes(reqStr)); }
public static void Post(AsynHttpContext context, byte[] reqBytes, Action <AsynHttpContext, Exception> callBack) { AysncHttpRequestHelperV2 proxy = new AysncHttpRequestHelperV2(context, callBack); proxy.SendRequest(reqBytes); }
public static void Get(AsynHttpContext context, Action <AsynHttpContext, Exception> callBack) { AysncHttpRequestHelperV2 proxy = new AysncHttpRequestHelperV2(context, callBack); proxy.SendRequest(); }
private AysncHttpRequestHelperV2(AsynHttpContext context, Action <AsynHttpContext, Exception> callBack) { this._context = context; this._action = callBack; this._watch = new Stopwatch(); }
static void Main(string[] args) { Action <string, Exception> OnResponseGet = new Action <string, Exception>((s, ex) => { if (ex != null) { string exInfo = ex.Message; } string ret = s; }); try { string strRequestXml = "hello"; int timeout = 15000; string contentType = "text/xml"; string url = "http://192.168.110.191:8092/getcontentinfo/"; UTF8Encoding encoding = new UTF8Encoding(); byte[] data = encoding.GetBytes(strRequestXml); HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); myHttpWebRequest.Timeout = timeout; myHttpWebRequest.Method = "POST"; myHttpWebRequest.ContentType = contentType; myHttpWebRequest.ContentLength = data.Length; //if (headers != null && headers.Count > 0) //{ // foreach (var eachheader in headers) // { // myHttpWebRequest.Headers.Add(eachheader.Key, eachheader.Value); // } //} AsynHttpContext asynContext = new AsynHttpContext(myHttpWebRequest); // string tranKey = TransactionManager<AsynHttpContext>.Instance.Register(asynContext); AysncHttpRequestHelperV2.Post(asynContext, data, new Action <AsynHttpContext, Exception>((httpContext, ex) => { try { // TransactionManager<AsynHttpContext>.Instance.Unregister(tranKey); if (ex != null) { // _tracing.Error(ex, "Exception Occurs On AysncHttpRequestHelperV2 Post Request."); OnResponseGet(null, ex); } else { string rspStr = Encoding.UTF8.GetString(httpContext.ResponseBytes); // _tracing.InfoFmt("Aysnc Get Response Content : {0}", rspStr); OnResponseGet(rspStr, null); } } catch (Exception ex2) { // _tracing.ErrorFmt(ex2, ""); OnResponseGet(null, ex2); } })); } catch (Exception ex) { // _tracing.Error(ex, "Exception Occurs On Aysnc Post Request."); OnResponseGet(null, ex); } Thread.Sleep(5000); }