C#实现的HttpSQS客户端封装类

class HttpSQSClient
    {
        private String server, port, charset;

        public HttpSQSClient(String server, String port, String charset)
        {
            this.server = server;
            this.port = port;
            this.charset = charset;
        }

        private string DoProcess(String urlstr)
        {
            string result = String.Empty;

            Uri url = null;
            try
            {
                url = new Uri(urlstr);
            }
            catch (Exception e)
            {
                return "The httpsqs server must be error";
            }

            try
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.Method = "get";

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                System.Text.Encoding encode;
                try
                {
                    encode = System.Text.Encoding.GetEncoding(response.CharacterSet);
                }
                catch
                {
                    encode = System.Text.Encoding.Default;
                }

                System.IO.Stream stream = response.GetResponseStream();
                System.IO.StreamReader read = new System.IO.StreamReader(stream, encode);
                result = read.ReadToEnd();

                response.Close();

                return result;
            }
            catch (IOException ex)
            {
                return "Get data error";
            }
        }

        public String MaxQueue(String queue_name, String num)
        {
            String urlstr = "http://" + this.server + ":" + this.port + "/?name="
                    + queue_name + "&opt=maxqueue&num=" + num;

            String result = null;

            result = this.DoProcess(urlstr);
            return result;
        }

        public String Reset(String queue_name)
        {
            String urlstr = "http://" + this.server + ":" + this.port + "/?name="
                    + queue_name + "&opt=reset";
            String result = null;

            result = this.DoProcess(urlstr);
            return result;
        }

        public String View(String queue_name, String pos)
        {
            String urlstr = "http://" + this.server + ":" + this.port
                    + "/?charset=" + this.charset + "&name=" + queue_name
                    + "&opt=view&pos=" + pos;
            String result = null;

            result = this.DoProcess(urlstr);
            return result;
        }

        public String Status(String queue_name)
        {
            String urlstr = "http://" + this.server + ":" + this.port + "/?name="
                    + queue_name + "&opt=status";
            String result = null;

            result = this.DoProcess(urlstr);
            return result;
        }

        public String Get(String queue_name)
        {
            String urlstr = "http://" + this.server + ":" + this.port
                    + "/?charset=" + this.charset + "&name=" + queue_name
                    + "&opt=get";
            String result = null;

            result = this.DoProcess(urlstr);
            return result;
        }

        public String Put(String queue_name, String data)
        {
            String ret = String.Empty;
            String urlstr = "http://" + this.server + ":" + this.port + "/?name="
                    + queue_name + "&opt=put";
            Uri url = null;
            try
            {
                url = new Uri(urlstr);
            }
            catch (Exception)
            {
                return "The httpsqs server must be error";
            }

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "post";
            byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(data);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = requestBytes.Length;
            request.Timeout = 6000;
            try
            {
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(requestBytes, 0, requestBytes.Length);
                requestStream.Close();

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);
                ret = reader.ReadToEnd();
            }
            catch (Exception)
            {
                return "Put data error";
            }

            return ret;
        }
    }

 

posted on 2012-12-14 18:00  jenqz  阅读(456)  评论(0编辑  收藏  举报