public void GetData(object sender, string sql, DataDownloadedHandler dataDownloaded, ServiceExceptionHandler errorReceived) { string uriString = String.Format(ServicePoint + "/Services/GetData.ashx?sql={0}", sql); HttpWebRequest client = WebRequest.CreateHttp(uriString); client.BeginGetResponse((async) => { HttpWebResponse res=null; try { res = (HttpWebResponse)client.EndGetResponse(async); } catch (Exception v) { Exception vv=new Exception(v.Message + " URL : " + uriString); if (errorReceived != null) errorReceived(async.AsyncState, new ServiceException(EErrorType.REQUEST, "Response error.")); return; } if (res.ContentType.Contains("text/xml")) { XDocument document=null; try { using (GZipInputStream zip=new GZipInputStream(res.GetResponseStream())) { document = XDocument.Load(zip); zip.Close(); } } catch (Exception exc) { if (errorReceived != null) errorReceived(async.AsyncState, new ServiceException(EErrorType.DECOMPRESSION, exc.Message)); return; } dataDownloaded(async.AsyncState, document.Element("table")); } else { //REQUEST,SERVER,READER,NODATA Stream str = res.GetResponseStream(); string s=String.Empty; string[] ss=null; string f=String.Empty; using (StreamReader sr = new StreamReader(str)) { s = sr.ReadToEnd(); } if (s != String.Empty) ss = s.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); EErrorType et=EErrorType.UNKNOWN; f = ss == null ? "UNKNOWN" : ss[0]; Enum.TryParse(f, out et); ServiceException ex=new ServiceException(et, ss[1]); if (errorReceived != null) errorReceived(async.AsyncState, ex); } }, sender); }
public void Execute(object sender, string sql, ExecutionCompletedHandler executionCompleted, ServiceExceptionHandler errorReceived) { string uriString = String.Format(ServicePoint + "/Services/execute.ashx?sql={0}", sql); HttpWebRequest client = WebRequest.CreateHttp(uriString); client.BeginGetResponse((async) => { HttpWebResponse res=null; try { res = (HttpWebResponse)client.EndGetResponse(async); } catch (Exception v) { Exception vv=new Exception(v.Message + " URL : " + uriString); if (errorReceived != null) errorReceived(async.AsyncState, new ServiceException(EErrorType.REQUEST, "Response error.")); return; } if (res.ContentType.Contains("text/result")) { int r=-1; Stream str=res.GetResponseStream(); using (StreamReader sr=new StreamReader(str)) { string s=sr.ReadToEnd(); int.TryParse(s, out r); } executionCompleted(async.AsyncState, r ); } else { //REQUEST,SERVER,READER,NODATA Stream str = res.GetResponseStream(); string s=String.Empty; string[] ss=null; string f=String.Empty; using (StreamReader sr = new StreamReader(str)) { s = sr.ReadToEnd(); } if (s != String.Empty) ss = s.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); EErrorType et=EErrorType.UNKNOWN; f = ss == null ? "UNKNOWN" : ss[0]; Enum.TryParse(f, out et); ServiceException ex=new ServiceException(et, ss[1]); if (errorReceived != null) errorReceived(async.AsyncState, ex); } }, sender); }