示例#1
0
        public override System.Xml.XmlNode ToXml(System.Xml.XmlNode node)
        {
            var ctl = node.OwnerDocument.CreateElement("barcode");

            Helper.XmlHelper.AddAttribute("encoding", EncodeType.ToString(), ctl);
            Helper.XmlHelper.AddAttribute("text", Text, ctl);
            Helper.XmlHelper.AddAttribute("text-visible", ShowText.ToString(), ctl);
            return(base.ToXml(ctl));
        }
示例#2
0
        /// <summary>
        /// Posts/returns the specified decode based on object type, method, parameters and encoding.
        /// </summary>
        /// <returns>
        /// The JSON response string returned from the API.
        /// </returns>
        private static T Post <T> (Func <string, T> decode, string method, string parameters, EncodeType encoding = EncodeType.JSON)
        {
            // Use this to ignore cert warning when running through your IDE
            ServicePointManager.ServerCertificateValidationCallback = (se, cert, chain, sslerror) => { return(true); };

            WebRequest  _webRequest;
            WebResponse _webResponse;

            T _response = default(T);

            // Build API post URI
            string _url = string.Format("{0}{1}{2}", APIHandler.Environment, APIHandler.Version, method);

            //if (encoding == EncodeType.XML && !_url.EndsWith (".xml"))
            //	_url += ".xml";

            //Setup web request
            _webRequest = WebRequest.Create(_url);

            _webRequest.ContentType = "application/" + encoding.ToString().ToLower();
            _webRequest.Method      = "POST";

            // Set API auth information
            _webRequest.Credentials = Auth.Credentials;

            byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(parameters != null ? parameters : string.Empty);

            try {
                //Wrap in using statments to implement IDisposable
                using (Stream stream = _webRequest.GetRequestStream())
                    stream.Write(buffer, 0, buffer.Length);
                using (_webResponse = _webRequest.GetResponse()) {
                    using (Stream stream = _webResponse.GetResponseStream()) {
                        using (StreamReader reader = new StreamReader(stream)) {
                            string responseStr = reader.ReadToEnd();

                            if (decode != null)
                            {
                                _response = decode(responseStr);
                            }

                            return(_response);
                        }
                    }
                }
            } catch (WebException ex) {
                //Catch a couple web errors for example, or display generic eror by error code
                if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
                {
                    var resp = (HttpWebResponse)ex.Response;
                    if (resp.StatusCode == HttpStatusCode.NotFound)
                    {
                        // 404 Error
                        return(decode("404 error"));
                    }
                    else if (resp.StatusCode == HttpStatusCode.BadRequest)
                    {
                        // 400 Error
                        return(_response = decode("400 Error"));
                    }
                }
                // Return unhandled error
                return(_response = decode(string.Format("{0} Error", ex.Status)));
            }
        }