C#WebService 客戶端通過Http調用請求(轉)


1.webservice幫助類

public class WebServiceHelper
    {      
        public static string CallServiceByGet(string strURL)
        {  
           
            //創建一個HTTP請求
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
            request.Method="get";
            HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
            Stream s = response.GetResponseStream();
            //轉化為XML,自己進行處理
            XmlTextReader Reader = new XmlTextReader(s);
            Reader.MoveToContent();
            string strValue = Reader.ReadInnerXml();
            Reader.Close();
            strValue = strValue.Replace("<", "<");
            strValue = strValue.Replace(">", ">");
            return strValue;
        }
        public static string CallServiceByPost(string strURL,System.Collections.Specialized.StringDictionary parameters)
        {         
            //創建一個HTTP請求
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
            //Post請求方式
            request.Method = "POST";
            //內容類型
            request.ContentType = "application/x-www-form-urlencoded";
            //設置參數,並進行URL編碼
            StringBuilder codedString = new StringBuilder();
            foreach (string key in parameters.Keys)
            {
                codedString.Append(HttpUtility.UrlEncode(key));
                codedString.Append("=");
                codedString.Append(HttpUtility.UrlEncode(parameters[key]));
                codedString.Append("&");
            }
            string paraUrlCoded = codedString.Length == 0 ? string.Empty:codedString.ToString().Substring(0, codedString.Length - 1);
            //string paraUrlCoded = HttpUtility.UrlEncode("ProductId");
            //paraUrlCoded += "=" + HttpUtility.UrlEncode(this.textBox1.Text);
            byte[] payload;
            //將URL編碼后的字符串轉化為字節
            payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
            //設置請求的ContentLength
            request.ContentLength = payload.Length;
            //發送請求,獲得請求流
            Stream writer = request.GetRequestStream();
            //將請求參數寫入流
            writer.Write(payload, 0, payload.Length);
            //關閉請求流
            writer.Close();
            //獲得響應流
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream s = response.GetResponseStream();
            //轉化為XML,自己進行處理
            XmlTextReader Reader = new XmlTextReader(s);
            Reader.MoveToContent();
            string strValue = Reader.ReadInnerXml();
            Reader.Close();
            strValue = strValue.Replace("<", "<");
            strValue = strValue.Replace(">", ">");            
            return strValue;
        }  
    }

---------------------------------------------------------------------------------------------------------------

2.webservice方法

---------------------------------------------------------------------------------------------------------------

/// <summary>
    /// Service1 的摘要說明
    /// </summary>
    [WebService(Namespace = "http://127.0.0.1/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld1()
        {
            return "Hello World";
        }

        [WebMethod]
        public string HelloWorld2(string p1,string p2)
        {
            return string.Concat("Hello World", ",", p1, ",", p2);
        }
        [WebMethod]
        public string HelloWorld3(string datetime)
        {
            return string.Concat("Hello World", " today is ", datetime);
        }
    }

---------------------------------------------------------------------------------------------------------------

3.使用webservice幫助類調用webservice方法

---------------------------------------------------------------------------------------------------------------

//帶2個字符串參數的調用
            string url = "http://localhost/ByDIF.WebService/Service.asmx/HelloWorld2";
            StringDictionary parameters = new StringDictionary();
            parameters.Add("p1","123");
            parameters.Add("p2", "456");
            string message = WebServiceHelper.CallServiceByPost(url,parameters);
            System.Console.WriteLine(message);
            //不帶參數的調用
            url = "http://localhost/ByDIF.WebService/Service.asmx/HelloWorld1";
            parameters = new StringDictionary();
            message = WebServiceHelper.CallServiceByPost(url, parameters);
            System.Console.WriteLine(message);
            //帶一個日期字符串的調用
            url = "http://localhost/ByDIF.WebService/Service.asmx/HelloWorld3";
            parameters = new StringDictionary();
            parameters.Add("datetime", System.DateTime.Now.ToShortDateString());
            message = WebServiceHelper.CallServiceByPost(url, parameters);
            System.Console.WriteLine(message);
            System.Console.Read();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM