C#WebService 用戶端通過Http調用請求

來源:互聯網
上載者:User

標籤:

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();

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.