Exemple #1
0
        public void GetXML(object sender, string xmlPath, DataDownloadedHandler dataDownloaded, ExceptionHandler errorReceived)
        {
            string uriString =  String.Format(ServicePoint + xmlPath);

            WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
            WebClient client = new WebClient();
            client.Encoding = Encoding.UTF8;
            client.Credentials = new NetworkCredential("serviceuser", "password");
            client.UseDefaultCredentials = false;

            //Debug.WriteLine(uriString);

            client.OpenReadCompleted += (s, e) =>
            {

                if (!e.Cancelled && (e.Error == null))
                {

                    XDocument document = XDocument.Load(e.Result);
                    dataDownloaded(sender, document.Element("root"));
                    Thread.CurrentThread.Join(2);

                }
                else
                {
            #if DEBUG
                    //MessageBox.Show(e.Error.Message + "\r\n" + (e.Error.InnerException != null ? e.Error.InnerException.Message : ""));
            #endif
                    if (errorReceived != null) errorReceived(null, e.Error);
                }

            };
            try
            {
                client.OpenReadAsync(new Uri(uriString, UriKind.Absolute));
            }
            catch (Exception ex)
            {

            #if DEBUG
                MessageBox.Show(ex.Message + "\r\n" + (ex.InnerException != null ? ex.InnerException.Message : ""));
            #endif
            }
        }
Exemple #2
0
        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);
        }