1. 程式人生 > >以Get方式訪問wcf方法

以Get方式訪問wcf方法

      private string Get(string serviceAddress)
        {
            try
            {
                //建立服務方法
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
                //設定獲取資料的方式
                request.Method = "GET";
                //設定獲取資料的編碼方式
                request.ContentType = "application/json;charset=UTF-8";
                //讀取資料
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream stream = response.GetResponseStream();
                StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                string result = reader.ReadToEnd();
                //關閉檔案流
                reader.Close();
                stream.Close();
                return result;
            }
            catch (Exception e)
            {
                throw e;
            }
        }